Example #1
0
 void IParserNode.AppendChildNode(IParserNode Child)
 {
     if (Child == null)
     {
         return;
     }
     m_Childs.AddLast(Child);
 }
Example #2
0
        private static Char[] s_DualSeparators = { '-', '>', '<', '/', '!', '=', '&', '|' }; //  -> // >= <= == != && ||
        private void parseLine(String Line, int LineNo, IParserNode Tree)
        {
            IParserNode   LineNode = null;
            int           offset   = 0;
            int           previous = 0;
            string        found;
            int           foundSingle = 0;
            int           foundDual   = 0;
            bool          stop        = false;
            bool          next        = false;
            List <String> Stack       = new List <string>();

            Line = NormalizeWhitespace(Line);
            while (!stop)
            {
                next        = false;
                foundDual   = Line.IndexOfAny(s_DualSeparators, offset);
                foundSingle = Line.IndexOfAny(s_SingleSeparators, offset);

                if (foundDual >= 0 && foundDual <= foundSingle)
                {
                    next  = true;
                    found = Line.Substring(foundDual, 2);
                    if (found == "->") // after function parameter list
                    {
                    }
                    else if (found == "//") // line comment
                    {
                    }
                    else // its a single
                    {
                        next = false;
                    }
                }
                if (foundSingle >= 0 && !next)
                {
                    next  = true;
                    found = Line.Substring(foundSingle, 1);
                    if (found == " ")
                    {
                    }
                    else // its a single
                    {
                        next = false;
                    }
                }

                if (!next)
                {
                    stop = true;
                }
            }
        }
Example #3
0
 private void EvaluateNode(IParserNode node, StringBuilder output)
 {
     if (node is HtmlNode)
     {
         output.Append(((HtmlNode)node).Content);
     }
     else if (node is ExpressionNode)
     {
         EvaluateExpressionNode((ExpressionNode)node, output);
     }
     else if (node is InlineFunctionNode)
     {
         EvaluateInlineFunctionNode((InlineFunctionNode)node, output);
     }
     else if (node is BlockFunctionNode)
     {
         EvaluateBlockFunctionNode((BlockFunctionNode)node, output);
     }
     else if (node is ExpressionFunctionNode)
     {
         EvaluateExpressionFunctionNode((ExpressionFunctionNode)node, output);
     }
 }
Example #4
0
 FunctionNode(IParserNode Parent, String Name)
     : base(Parent)
 {
     m_Name = Name;
 }
Example #5
0
 VariableNode(IParserNode Parent, String Name, String Type) : base(Parent)
 {
     m_Name = Name;
     m_Type = Type;
 }
Example #6
0
 public ParserNode(IParserNode Parent)
 {
     m_Parent = Parent;
 }
Example #7
0
 public ParserNode()
 {
     m_Parent = null;
 }
Example #8
0
 public ParsePrefixSymbolsNode(IEnumerable <Symbol> prefixSymbols, IParserNode suffixNode)
 {
     this.PrefixSymbols = prefixSymbols.ToArray();
     this.SuffixNode    = suffixNode;
 }
Example #9
0
 public VariableSwitchNode(string variableName, IParserNode trueNode, IParserNode falseNode)
 {
     this.VariableName = variableName;
     this.TrueNode     = trueNode;
     this.FalseNode    = falseNode;
 }
Example #10
0
 public MapResultNode(IParserNode mapped, IEnumerable <KeyValuePair <Rule, IParserNode> > mapping)
 {
     this.Mapped  = mapped;
     this.Mapping = mapping.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
 }
Example #11
0
        private Rule Parse(IParserNode node)
        {
            this.debugWriter(
                $"{(this.IsInLookahead ? "LOOK " : "PARSE")} @{(this.IsInLookahead ? this.lookaheadIndex : this.index)}={this.Peek()}: {node.Kind} {node}"
                );

            switch (node.Kind)
            {
            case ParserNodeKind.ParseSymbol:
                return(this.Parse(((ParseSymbolNode)node).Symbol));

            case ParserNodeKind.ParseRule:
                var rule = ((ParseRuleNode)node).Rule;

                if (!this.IsInLookahead &&
                    rule.Rule.RequiredParserVariable != null &&
                    (
                        !this.stateVariables.TryGetValue(rule.Rule.RequiredParserVariable, out var values) ||
                        values.Count == 0 ||
                        !values.Peek()
                    ))
                {
                    throw new InvalidOperationException($"Cannot parse {rule.Rule} without variable {rule.Rule.RequiredParserVariable}");
                }

                foreach (var symbol in rule.Symbols)
                {
                    if (symbol is Token)
                    {
                        this.Eat((Token)symbol);
                    }
                    else
                    {
                        this.Parse((NonTerminal)symbol);
                    }
                }

                if (rule.Rule.Action != null)
                {
                    switch (rule.Rule.Action.Kind)
                    {
                    case RuleActionKind.Pop:
                        if (!this.stateVariables.TryGetValue(rule.Rule.Action.Variable, out var toPop) || toPop.Count == 0)
                        {
                            throw new InvalidOperationException($"Rule {rule.Rule} should pop {rule.Rule.Action.Variable} but the stack is empty");
                        }
                        toPop.Pop();
                        break;

                    case RuleActionKind.Push:
                        Stack <bool> toPush;
                        if (!this.stateVariables.TryGetValue(rule.Rule.Action.Variable, out toPush))
                        {
                            this.stateVariables.Add(rule.Rule.Action.Variable, toPush = new Stack <bool>());
                        }
                        toPush.Push(false);
                        break;

                    case RuleActionKind.Set:
                        if (!this.stateVariables.TryGetValue(rule.Rule.Action.Variable, out var toSet) || toSet.Count == 0)
                        {
                            throw new InvalidOperationException($"Rule {rule.Rule} should set {rule.Rule.Action.Variable} but the stack is empty");
                        }
                        toSet.Pop();
                        toSet.Push(true);
                        break;

                    default:
                        throw new InvalidOperationException(rule.Rule.Action.Kind.ToString());
                    }
                }

                return(rule.Rule);

            case ParserNodeKind.TokenLookahead:
                var mapping = ((TokenLookaheadNode)node).Mapping;
                var next    = this.Peek();
                return(this.Parse(mapping[next]));

            case ParserNodeKind.MapResult:
                var mapNode     = (MapResultNode)node;
                var innerResult = this.Parse(mapNode.Mapped);
                return(this.Parse(mapNode.Mapping[innerResult]));

            case ParserNodeKind.ParsePrefixSymbols:
                var prefixNode = (ParsePrefixSymbolsNode)node;
                foreach (var symbol in prefixNode.PrefixSymbols)
                {
                    if (symbol is Token)
                    {
                        this.Eat((Token)symbol);
                    }
                    else
                    {
                        this.Parse((NonTerminal)symbol);
                    }
                }
                return(this.Parse(prefixNode.SuffixNode));

            case ParserNodeKind.GrammarLookahead:
                var lookaheadNode = (GrammarLookaheadNode)node;
                if (this.IsInLookahead)
                {
                    this.Eat(lookaheadNode.Token);
                    var ruleUsed = this.Parse(lookaheadNode.Discriminator);
                    // TODO this potentially should perform any rule actions (e. g. state variables)
                    return(lookaheadNode.Mapping[ruleUsed]);
                }
                else
                {
                    this.lookaheadIndex = this.index;

                    this.Eat(lookaheadNode.Token);
                    var ruleUsed = this.Parse(lookaheadNode.Discriminator);

                    this.lookaheadIndex = -1;

                    return(this.Parse(new ParseRuleNode(lookaheadNode.Mapping[ruleUsed])));
                }

            case ParserNodeKind.VariableSwitch:
                var switchNode = (VariableSwitchNode)node;
                if (this.stateVariables.TryGetValue(switchNode.VariableName, out var variableValues) &&
                    variableValues.Count > 0 &&
                    variableValues.Peek())
                {
                    return(this.Parse(switchNode.TrueNode));
                }
                return(this.Parse(switchNode.FalseNode));

            default:
                throw new ArgumentException(node.Kind.ToString());
            }
        }
Example #12
0
        private Rule Parse(IParserNode node)
        {
            this.debugWriter(
                $"{(this.IsInLookahead ? "LOOK " : "PARSE")} @{(this.IsInLookahead ? this.lookaheadIndex : this.index)}={this.Peek()}: {node.Kind} {node}"
                );

            switch (node.Kind)
            {
            case ParserNodeKind.ParseSymbol:
                return(this.Parse(((ParseSymbolNode)node).Symbol));

            case ParserNodeKind.ParseRule:
                var rule = ((ParseRuleNode)node).Rule;
                foreach (var symbol in rule.Symbols)
                {
                    if (symbol is Token)
                    {
                        this.Eat((Token)symbol);
                    }
                    else
                    {
                        this.Parse((NonTerminal)symbol);
                    }
                }
                return(rule.Rule);

            case ParserNodeKind.TokenLookahead:
                var mapping = ((TokenLookaheadNode)node).Mapping;
                var next    = this.Peek();
                return(this.Parse(mapping[next]));

            case ParserNodeKind.MapResult:
                var mapNode     = (MapResultNode)node;
                var innerResult = this.Parse(mapNode.Mapped);
                return(this.Parse(mapNode.Mapping[innerResult]));

            case ParserNodeKind.ParsePrefixSymbols:
                var prefixNode = (ParsePrefixSymbolsNode)node;
                foreach (var symbol in prefixNode.PrefixSymbols)
                {
                    if (symbol is Token)
                    {
                        this.Eat((Token)symbol);
                    }
                    else
                    {
                        this.Parse((NonTerminal)symbol);
                    }
                }
                return(this.Parse(prefixNode.SuffixNode));

            case ParserNodeKind.GrammarLookahead:
                var lookaheadNode = (GrammarLookaheadNode)node;
                if (this.IsInLookahead)
                {
                    this.Eat(lookaheadNode.Token);
                    var ruleUsed = this.Parse(lookaheadNode.Discriminator);
                    return(lookaheadNode.Mapping[ruleUsed]);
                }
                else
                {
                    this.lookaheadIndex = this.index;

                    this.Eat(lookaheadNode.Token);
                    var ruleUsed = this.Parse(lookaheadNode.Discriminator);

                    this.lookaheadIndex = -1;

                    return(this.Parse(new ParseRuleNode(lookaheadNode.Mapping[ruleUsed])));
                }

            default:
                throw new ArgumentException(node.Kind.ToString());
            }
        }