packages/extension-map/doc/webpacks/shexmap-webapp.js
/* _compileShapeToAST - compile a shape expression to an abstract syntax tree.
     *
     * currently tested but not used.
     */
compile a shape expression to an abstract syntax tree.
currently tested but not used.
compile a shape expression to an abstract syntax tree . currently tested but not used .
function _compileShapeToAST (expression, tripleConstraints, schema) {
      function Epsilon () {
        this.type = "Epsilon";
      }
      function TripleConstraint (ordinal, predicate, inverse, negated, valueExpr) {
        this.type = "TripleConstraint";
        this.inverse = !!inverse;
        this.negated = !!negated;
        this.predicate = predicate;
        if (valueExpr !== undefined)
          this.valueExpr = valueExpr;
      }
      function Choice (disjuncts) {
        this.type = "Choice";
        this.disjuncts = disjuncts;
      }
      function EachOf (conjuncts) {
        this.type = "EachOf";
        this.conjuncts = conjuncts;
      }
      function SemActs (expression, semActs) {
        this.type = "SemActs";
        this.expression = expression;
        this.semActs = semActs;
      }
      function KleeneStar (expression) {
        this.type = "KleeneStar";
        this.expression = expression;
      }
      function _compileExpression (expr, schema) {
        let repeated, container;
        function _repeat (expr, min, max) {
          if (min === undefined) { min = 1; }
          if (max === undefined) { max = 1; }
          if (min === 1 && max === 1) { return expr; }
          const opts = max === UNBOUNDED ?
                new KleeneStar(expr) :
                Array.from(Array(max - min)).reduce(function (ret, elt, ord) {
                  return ord === 0 ?
                    new Choice([expr, new Epsilon]) :
                    new Choice([new EachOf([expr, ret]), new Epsilon]);
                }, undefined);
          const reqd = min !== 0 ?
                new EachOf(Array.from(Array(min)).map(function (ret) {
                  return expr; 
                }).concat(opts)) : opts;
          return reqd;
        }
        if (typeof expr === "string") { 
          const included = schema._index.tripleExprs[expr].expression;
          return _compileExpression(included, schema);
        }
        else if (expr.type === "TripleConstraint") {
          const valueExpr = "valueExprRef" in expr ?
                schema.valueExprDefns[expr.valueExprRef] :
                expr.valueExpr;
          const ordinal = tripleConstraints.push(expr)-1;
          const tp = new TripleConstraint(ordinal, expr.predicate, expr.inverse, expr.negated, valueExpr);
          repeated = _repeat(tp, expr.min, expr.max);
          return expr.semActs ? new SemActs(repeated, expr.semActs) : repeated;
        }
        else if (expr.type === "OneOf") {
          container = new Choice(expr.expressions.map(function (e) {
            return _compileExpression(e, schema);
          }));
          repeated = _repeat(container, expr.min, expr.max);
          return expr.semActs ? new SemActs(repeated, expr.semActs) : repeated;
        }
        else if (expr.type === "EachOf") {
          container = new EachOf(expr.expressions.map(function (e) {
            return _compileExpression(e, schema);
          }));
          repeated = _repeat(container, expr.min, expr.max);
          return expr.semActs ? new SemActs(repeated, expr.semActs) : repeated;
        }
        else throw Error("unexpected expr type: " + expr.type);
      }
      return expression ? _compileExpression(expression, schema) : new Epsilon();
    }
function _compileShapeToAST ( expression , tripleConstraints , schema ) { function Epsilon ( ) { this . type = "Epsilon" ; } function TripleConstraint ( ordinal , predicate , inverse , negated , valueExpr ) { this . type = "TripleConstraint" ; this . inverse = ! ! inverse ; this . negated = ! ! negated ; this . predicate = predicate ; if ( valueExpr !== undefined ) this . valueExpr = valueExpr ; } function Choice ( disjuncts ) { this . type = "Choice" ; this . disjuncts = disjuncts ; } function EachOf ( conjuncts ) { this . type = "EachOf" ; this . conjuncts = conjuncts ; } function SemActs ( expression , semActs ) { this . type = "SemActs" ; this . expression = expression ; this . semActs = semActs ; } function KleeneStar ( expression ) { this . type = "KleeneStar" ; this . expression = expression ; } function _compileExpression ( expr , schema ) { let repeated , container ; function _repeat ( expr , min , max ) { if ( min === undefined ) { min = 1 ; } if ( max === undefined ) { max = 1 ; } if ( min === 1 && max === 1 ) { return expr ; } const opts = max === UNBOUNDED ? new KleeneStar ( expr ) : Array . from ( Array ( max - min ) ) . reduce ( function ( ret , elt , ord ) { return ord === 0 ? new Choice ( [ expr , new Epsilon ] ) : new Choice ( [ new EachOf ( [ expr , ret ] ) , new Epsilon ] ) ; } , undefined ) ; const reqd = min !== 0 ? new EachOf ( Array . from ( Array ( min ) ) . map ( function ( ret ) { return expr ; } ) . concat ( opts ) ) : opts ; return reqd ; } if ( typeof expr === "string" ) { const included = schema . _index . tripleExprs [ expr ] . expression ; return _compileExpression ( included , schema ) ; } else if ( expr . type === "TripleConstraint" ) { const valueExpr = "valueExprRef" in expr ? schema . valueExprDefns [ expr . valueExprRef ] : expr . valueExpr ; const ordinal = tripleConstraints . push ( expr ) - 1 ; const tp = new TripleConstraint ( ordinal , expr . predicate , expr . inverse , expr . negated , valueExpr ) ; repeated = _repeat ( tp , expr . min , expr . max ) ; return expr . semActs ? new SemActs ( repeated , expr . semActs ) : repeated ; } else if ( expr . type === "OneOf" ) { container = new Choice ( expr . expressions . map ( function ( e ) { return _compileExpression ( e , schema ) ; } ) ) ; repeated = _repeat ( container , expr . min , expr . max ) ; return expr . semActs ? new SemActs ( repeated , expr . semActs ) : repeated ; } else if ( expr . type === "EachOf" ) { container = new EachOf ( expr . expressions . map ( function ( e ) { return _compileExpression ( e , schema ) ; } ) ) ; repeated = _repeat ( container , expr . min , expr . max ) ; return expr . semActs ? new SemActs ( repeated , expr . semActs ) : repeated ; } else throw Error ( "unexpected expr type: " + expr . type ) ; } return expression ? _compileExpression ( expression , schema ) : new Epsilon ( ) ; }
_compileShapeToAST - compile a shape expression to an abstract syntax tree.
_compileShapeToAST - compile a shape expression to an abstract syntax tree .
// this.ordinal = ordinal; @@ does 1card25 /* _repeat: map expr with a min and max cardinality to a corresponding AST with Groups and Stars.
           expr 1 1 => expr
           expr 0 1 => Choice(expr, Eps)
           expr 0 3 => Choice(EachOf(expr, Choice(EachOf(expr, Choice(expr, EPS)), Eps)), Eps)
           expr 2 5 => EachOf(expr, expr, Choice(EachOf(expr, Choice(EachOf(expr, Choice(expr, EPS)), Eps)), Eps))
           expr 0 * => KleeneStar(expr)
           expr 1 * => EachOf(expr, KleeneStar(expr))
           expr 2 * => EachOf(expr, expr, KleeneStar(expr))

           @@TODO: favor Plus over Star if Epsilon not in expr.
        */ // @@ something with ret // Inclusion // predicate, inverse, negated, valueExpr, annotations, semActs, min, max