示例#1
0
        private void HandleDelimiter(Delim delim, ref int index, List <Context.Entry> contextStack,
                                     ref Context currentContext, Context defaultContext)
        {
            Token delimToken = new Token(delim, index, delim.text.Length);

            if (delim.parseRule != null)
            {
                ParseResult pr = delim.parseRule.Invoke(str, index);
                if (pr.IsError && errors != null)
                {
                    pr.error.OffsetBy(delimToken.index, rows);
                    errors.Add(pr.error);
                }
                if (pr.replacementValue != null)
                {
                    delimToken.length = pr.lengthParsed;
                    delimToken.meta   = new TokenSubstitution(str, pr.replacementValue);
                }
                index += pr.lengthParsed - 1;
            }
            else
            {
                index += delim.text.Length - 1;
            }
            DelimCtx dcx = delim as DelimCtx;

            Context.Entry endedContext = null;
            if (dcx != null)
            {
                if (contextStack.Count > 0 && dcx.Context == currentContext && dcx.isEnd)
                {
                    endedContext            = contextStack[contextStack.Count - 1];
                    endedContext.endDelim   = dcx;
                    delimToken.meta         = endedContext;
                    endedContext.tokenCount = (tokens.Count - endedContext.tokenStart) + 1;
                    contextStack.RemoveAt(contextStack.Count - 1);
                    if (contextStack.Count > 0)
                    {
                        currentContext = contextStack[contextStack.Count - 1].context;
                    }
                    else
                    {
                        currentContext = defaultContext;
                    }
                }
                if (endedContext == null && dcx.isStart)
                {
                    Context.Entry parentCntx = (contextStack.Count > 0) ? contextStack[contextStack.Count - 1] : null;
                    Context.Entry newContext = dcx.Context.GetEntry(tokens, tokens.Count, str, parentCntx);
                    newContext.beginDelim = dcx;
                    currentContext        = dcx.Context;
                    delimToken.meta       = newContext;
                    contextStack.Add(newContext);
                }
            }
            tokens.Add(delimToken);
            if (endedContext != null)
            {
                ExtractContextAsSubTokenList(endedContext);
            }
        }
示例#2
0
        private void HandleDelimiter(Delim delim, ref int index, List <SyntaxTree> syntaxStack,
                                     ref ParseRuleSet currentContext, ParseRuleSet defaultContext)
        {
            Token delimToken = new Token(delim, index, delim.text.Length);

            if (delim.parseRule != null)
            {
                ParseResult pr = delim.parseRule.Invoke(str, index);
                if (pr.IsError && errors != null)
                {
                    pr.error.OffsetBy(delimToken.index, rows);
                    errors.Add(pr.error);
                }
                if (pr.replacementValue != null)
                {
                    delimToken.length = pr.lengthParsed;
                    delimToken.meta   = new TokenSubstitution(str, pr.replacementValue);
                }
                index += pr.lengthParsed - 1;
            }
            else
            {
                index += delim.text.Length - 1;
            }
            DelimCtx   dcx         = delim as DelimCtx;
            SyntaxTree endedSyntax = null;

            if (dcx != null)
            {
                if (syntaxStack.Count > 0 && dcx.Context == currentContext && dcx.isEnd)
                {
                    endedSyntax            = syntaxStack[syntaxStack.Count - 1];
                    endedSyntax.endDelim   = dcx;
                    delimToken.meta        = endedSyntax;
                    endedSyntax.tokenCount = (tokens.Count - endedSyntax.tokenStart) + 1;
                    syntaxStack.RemoveAt(syntaxStack.Count - 1);
                    if (syntaxStack.Count > 0)
                    {
                        currentContext = syntaxStack[syntaxStack.Count - 1].rules;
                    }
                    else
                    {
                        currentContext = defaultContext;
                    }
                }
                if (endedSyntax == null && dcx.isStart)
                {
                    SyntaxTree parentCntx = (syntaxStack.Count > 0) ? syntaxStack[syntaxStack.Count - 1] : null;
                    SyntaxTree newContext = dcx.Context.GetEntry(tokens, tokens.Count, str, parentCntx);
                    newContext.beginDelim = dcx;
                    currentContext        = dcx.Context;
                    delimToken.meta       = newContext;
                    syntaxStack.Add(newContext);
                }
            }
            tokens.Add(delimToken);
            tokenStrings.Add(delim.text);
            if (endedSyntax != null)
            {
                ExtractContextAsSubTokenList(endedSyntax);
            }
        }