Esempio n. 1
0
    public override bool PreCheck(ParseNode_Base node, int maxDistance = int.MaxValue)
    {
        if (m_kTokenScanner.IsEnding)
        {
            return(false);
        }
        var line  = m_kTokenScanner.CurrentLine;
        var index = m_kTokenScanner.CurrentTokenIndex;
        //	var realIndex = nonTriviaTokenIndex;

        var temp = maxScanDistance;

        maxScanDistance = maxDistance;
        var match = node.Scan(this);

        maxScanDistance = temp;

        //for (var i = m_kTokenScanner.CurrentLine; i > line; --i)
        //    if (i < lines.Length)
        //        lines[i].laLines = Math.Max(lines[i].laLines, i - line);

        m_kTokenScanner.MoveTo(line, index);
        //m_kTokenScanner.CurrentLine = line;
        //m_kTokenScanner.CurrentTokenIndex = index;
        //	nonTriviaTokenIndex = realIndex;
        //tokens = currentLine < lines.Length ? lines[currentLine].tokens : null;
        return(match);
    }
    public override ParseNode_Base NextAfterChild(ParseNode_Base child, SyntaxTreeBuilder pSyntaxTreeBuilder)
    {
        //	if (pSyntaxTreeBuilder.ErrorMessage == null || Parse(pSyntaxTreeBuilder.Clone()))
        return(this);

        //	return base.NextAfterChild(child, pSyntaxTreeBuilder);
    }
Esempio n. 3
0
    public override sealed void Add(ParseNode_Base node)
    {
        var idNode = node as ParseNode_Id;

        if (idNode != null)
        {
            node = idNode.Clone();
        }

        var seqNode = node as ParseNode_Seq;

        if (seqNode != null)
        {
            foreach (var n in seqNode.nodes)
            {
                Add(n);
            }
        }
        else
        {
            node.parent     = this;
            node.childIndex = nodes.Count;
            nodes.Add(node);
        }
    }
    public override ParseNode_Base NextAfterChild(ParseNode_Base child, SyntaxTreeBuilder pSyntaxTreeBuilder)
    {
        var SyntaxRule_Cur = pSyntaxTreeBuilder.SyntaxRule_Cur;

        if (SyntaxRule_Cur == null)
        {
            return(null);
        }
        var res = SyntaxRule_Cur.ParseNode != null?SyntaxRule_Cur.ParseNode.NextAfterChild(this, pSyntaxTreeBuilder) : null;

        if (pSyntaxTreeBuilder.Seeking)
        {
            return(res);
        }

        if (contextualKeyword && SyntaxRule_Cur.NumValidNodes == 1)
        {
            var token = SyntaxRule_Cur.LeafAt(0).token;
            token.tokenKind = LexerToken.Kind.ContextualKeyword;
        }
        if (SyntaxRule_Cur.Semantics != SemanticFlags.None)
        {
            pSyntaxTreeBuilder.OnSemanticNodeClose(SyntaxRule_Cur);
        }
        return(res);
    }
    public override sealed void Add(ParseNode_Base node)
    {
        var idNode = node as ParseNode_Id;

        if (idNode != null)
        {
            node = idNode.Clone();
        }

        var altNode = node as ParseNode_Alt;

        if (altNode != null)
        {
            foreach (var n in altNode.nodes)
            {
                n.parent = this;
                nodes.Add(n);
            }
        }
        else
        {
            node.parent = this;
            nodes.Add(node);
        }
    }
 public void _AR_(ParseNode_Id id, ParseNode_Base rhs, SemanticFlags kSF = 0, bool bContextualKeyword = false, bool bAuto = false)
 {
     m_kParseRoot.AddRule(new ParseNode_Rule(id.Name, rhs)
     {
         semantics = kSF, contextualKeyword = bContextualKeyword, autoExclude = bAuto
     });
 }
Esempio n. 7
0
    public override ParseNode_Base NextAfterChild(ParseNode_Base child, SyntaxTreeBuilder pSyntaxTreeBuilder)
    {
        var index = child.childIndex;

        if (++index < nodes.Count)
        {
            return(nodes[index]);
        }
        return(base.NextAfterChild(this, pSyntaxTreeBuilder));
    }
    public ParseNode_Rule(string nt, ParseNode_Base rhs)
    {
        var idNode = rhs as ParseNode_Id;

        if (idNode != null)
        {
            rhs = idNode.Clone();
        }
        this.m_sNtName = nt;
        rhs.parent     = this;
        this.rhs       = rhs;
    }
    public ParseNode_Many(ParseNode_Base node)
    {
        var idNode = node as ParseNode_Id;

        if (idNode != null)
        {
            node = idNode.Clone();
        }

        node.parent = this;
        this.node   = node;
    }
Esempio n. 10
0
 public override ParseNode_Base NextAfterChild(ParseNode_Base child, SyntaxTreeBuilder pSyntaxTreeBuilder)
 {
     try
     {
         if (LinkedTarget is ParseNode_Rule)
         {
             pSyntaxTreeBuilder.SyntaxRule_Cur = pSyntaxTreeBuilder.SyntaxRule_Cur.Parent;
         }
         return(base.NextAfterChild(this, pSyntaxTreeBuilder));
     }
     catch (System.Exception)
     {
         throw;
     }
 }
    public override ParseNode_Base Parse(SyntaxTreeBuilder pSyntaxTreeBuilder)
    {
        bool           wasError = pSyntaxTreeBuilder.ErrorMessage != null;
        ParseNode_Base ret      = null;

        if (FirstSet.Contains(pSyntaxTreeBuilder.TokenScanner.Current))
        {
            ret = rhs.Parse(pSyntaxTreeBuilder);
        }
        if ((ret == null || !wasError && pSyntaxTreeBuilder.ErrorMessage != null) && !FirstSet.ContainsEmpty())
        {
            pSyntaxTreeBuilder.SyntaxErrorExpected(FirstSet);
            return(ret ?? this);
        }
        if (ret != null)
        {
            return(ret);
        }
        return(NextAfterChild(rhs, pSyntaxTreeBuilder)); // ready to be reduced
    }
Esempio n. 12
0
 public ParseNode_ManyOpt_If IF(Predicate <SyntaxTreeBuilder> pred, ParseNode_Base node, bool debug = false)
 {
     return(new ParseNode_ManyOpt_If(pred, node, debug));
 }
Esempio n. 13
0
 public ParseNode_ManyOpt_If IF(ParseNode_Base pred, ParseNode_Base node, bool debug = false)
 {
     return(new ParseNode_ManyOpt_If(pred, node, debug));
 }
Esempio n. 14
0
 public ParseNode_ManyOpt_IfNot NIF(ParseNode_Base pred, ParseNode_Base node)
 {
     return(new ParseNode_ManyOpt_IfNot(pred, node));
 }
    public bool ParseStep(SyntaxTreeBuilder pSyntaxTreeBuilder)
    {
        if (pSyntaxTreeBuilder.ParseNode_Cur == null)
        {
            return(false);
        }

        var token = pSyntaxTreeBuilder.TokenScanner.Current;

        if (pSyntaxTreeBuilder.ErrorMessage == null)
        {
            while (pSyntaxTreeBuilder.ParseNode_Cur != null)
            {
                pSyntaxTreeBuilder.ParseNode_Cur = pSyntaxTreeBuilder.ParseNode_Cur.Parse(pSyntaxTreeBuilder);
                if (pSyntaxTreeBuilder.ErrorMessage != null || token != pSyntaxTreeBuilder.TokenScanner.Current)
                {
                    break;
                }
            }

            if (pSyntaxTreeBuilder.ErrorMessage == null && token != pSyntaxTreeBuilder.TokenScanner.Current)
            {
                pSyntaxTreeBuilder.SyntaxRule_Err = pSyntaxTreeBuilder.SyntaxRule_Cur;
                pSyntaxTreeBuilder.ParseNode_Err  = pSyntaxTreeBuilder.ParseNode_Cur;
            }
        }
        if (pSyntaxTreeBuilder.ErrorMessage != null)
        {
            if (token.tokenKind == LexerToken.Kind.EOF)
            {
                return(false);
            }

            var missingParseTreeNode = pSyntaxTreeBuilder.SyntaxRule_Cur;
            var missingParseNode     = pSyntaxTreeBuilder.ParseNode_Cur;

            pSyntaxTreeBuilder.SyntaxRule_Cur = pSyntaxTreeBuilder.SyntaxRule_Err;
            pSyntaxTreeBuilder.ParseNode_Cur  = pSyntaxTreeBuilder.ParseNode_Err;
            if (pSyntaxTreeBuilder.SyntaxRule_Cur != null)
            {
                var cpt = pSyntaxTreeBuilder.SyntaxRule_Cur;
                for (var i = cpt.NumValidNodes; i > 0 && !cpt.ChildAt(--i).HasLeafs();)
                {
                    cpt.InvalidateFrom(i);
                }
            }

            if (pSyntaxTreeBuilder.ParseNode_Cur != null)
            {
                int numSkipped;
                pSyntaxTreeBuilder.ParseNode_Cur = pSyntaxTreeBuilder.ParseNode_Cur.Recover(pSyntaxTreeBuilder, out numSkipped);
            }
            if (pSyntaxTreeBuilder.ParseNode_Cur == null)
            {
                if (token.m_kLinkedLeaf != null)
                {
                    token.m_kLinkedLeaf.ReparseToken();
                }
                new SyntaxTreeNode_Leaf(pSyntaxTreeBuilder.TokenScanner);

                if (cachedErrorParseNode == pSyntaxTreeBuilder.ParseNode_Err)
                {
                    token.m_kLinkedLeaf.m_sSyntaxError = cachedErrorMessage;
                }
                else
                {
                    token.m_kLinkedLeaf.m_sSyntaxError = "Unexpected token! Expected " + pSyntaxTreeBuilder.ParseNode_Err.FirstSet.ToString(this);
                    cachedErrorMessage   = token.m_kLinkedLeaf.m_sSyntaxError;
                    cachedErrorParseNode = pSyntaxTreeBuilder.ParseNode_Err;
                }

                pSyntaxTreeBuilder.ParseNode_Cur  = pSyntaxTreeBuilder.ParseNode_Err;
                pSyntaxTreeBuilder.SyntaxRule_Cur = pSyntaxTreeBuilder.SyntaxRule_Err;

                if (!pSyntaxTreeBuilder.TokenScanner.MoveNext())
                {
                    return(false);
                }
                pSyntaxTreeBuilder.ErrorMessage = null;
            }
            else
            {
                if (missingParseNode != null && missingParseTreeNode != null)
                {
                    pSyntaxTreeBuilder.SyntaxRule_Cur = missingParseTreeNode;
                    pSyntaxTreeBuilder.ParseNode_Cur  = missingParseNode;
                }

                pSyntaxTreeBuilder.InsertMissingToken(pSyntaxTreeBuilder.ErrorMessage
                                                      ?? ("Expected " + missingParseNode.FirstSet.ToString(this)));

                if (missingParseNode != null && missingParseTreeNode != null)
                {
                    pSyntaxTreeBuilder.ErrorMessage   = null;
                    pSyntaxTreeBuilder.ErrorToken     = null;
                    pSyntaxTreeBuilder.SyntaxRule_Cur = missingParseTreeNode;
                    pSyntaxTreeBuilder.ParseNode_Cur  = missingParseNode;
                    pSyntaxTreeBuilder.ParseNode_Cur  = missingParseNode.parent.NextAfterChild(missingParseNode, pSyntaxTreeBuilder);
                }
                pSyntaxTreeBuilder.ErrorMessage = null;
                pSyntaxTreeBuilder.ErrorToken   = null;
            }
        }

        return(true);
    }
 public ParseNode_Many_Opt(ParseNode_Base node)
     : base(node)
 {
 }
 public ParseNode_ManyOpt_If(ParseNode_Base pred, ParseNode_Base node, bool debug = false)
     : base(node)
 {
     nodePredicate = pred;
     this.debug    = debug;
 }
 public ParseNode_ManyOpt_If(Predicate <SyntaxTreeBuilder> pred, ParseNode_Base node, bool debug = false)
     : base(node)
 {
     predicate  = pred;
     this.debug = debug;
 }
Esempio n. 19
0
 public ParseNode_Alt Alt(ParseNode_Base node)
 {
     return(new ParseNode_Alt(node));
 }
 public virtual void Add(ParseNode_Base node)
 {
     throw new Exception(GetType() + ": cannot Add()");
 }
Esempio n. 21
0
 public abstract bool PreCheck(ParseNode_Base node, int maxDistance   = int.MaxValue);
Esempio n. 22
0
 public ParseNode_Many Many(ParseNode_Base node)
 {
     return(new ParseNode_Many(node));
 }
Esempio n. 23
0
 public ParseNode_Many_Opt Opt(ParseNode_Base node)
 {
     return(new ParseNode_Many_Opt(node));
 }
 public virtual ParseNode_Base NextAfterChild(ParseNode_Base child, SyntaxTreeBuilder pSyntaxTreeBuilder)
 {
     return(parent != null?parent.NextAfterChild(this, pSyntaxTreeBuilder) : null);
 }
Esempio n. 25
0
    //public IfNot(Predicate<IpSyntaxTreeBuilder> pred, Node node)
    //    : base(pred, node)
    //{
    //}

    public ParseNode_ManyOpt_IfNot(ParseNode_Base pred, ParseNode_Base node)
        : base(pred, node)
    {
    }