protected override object OnRuleExpression(INonterminalToken token) { if (token == null) { throw new ArgumentNullException(nameof(token)); } var expression = Visit <IExpression>(token[0]); var children = token.Nonterminal(1).Children; if (children.Count > 0) { var expressions = new List <IExpression> { expression }; foreach (var child in children) { expressions.Add(Visit <IExpression>(child)); } expression = new Alternates(expressions); } return(expression); }
protected override object OnGrammarRules(INonterminalToken token) { if (token == null) { throw new ArgumentNullException(nameof(token)); } return(token.Nonterminal(0).Children.Select(Visit <NonterminalRule>).ToArray()); }
protected override object OnRuleSequence(INonterminalToken token) { var children = token.Nonterminal(0).Children; var expressions = children.Select(Visit <IExpression>).ToArray(); if (expressions.Length > 1) { return(new Sequence(expressions)); } return(expressions[0]); }
protected override object OnGrammar(INonterminalToken token) { Debug.Assert(token[0].Symbol.Id == LinguId.Identifier); var grammar = new RawGrammar(token.Terminal(0).Value); foreach (var section in token.Nonterminal(1).Children) { var children = Visit <IReadOnlyList <Symbol> >(section); grammar.Options.AddRange(children.OfType <Option>()); grammar.Terminals.AddRange(children.OfType <TerminalRule>()); grammar.Nonterminals.AddRange(children.OfType <NonterminalRule>()); } return(grammar); }
protected override object OnTerminalSequence(INonterminalToken token) { if (token == null) { throw new ArgumentNullException(nameof(token)); } var children = token.Nonterminal(0).Children; var expressions = children.Select(Visit <IExpression>).ToArray(); if (expressions.Length > 1) { return(new Sequence(expressions)); } return(expressions[0]); }
protected override object OnTerminalExpression(INonterminalToken token) { var expressions = new List <IExpression>() { Visit <IExpression>(token[0]) }; var children = token.Nonterminal(1).Children; expressions.AddRange(children.Select(Visit <IExpression>)); if (expressions.Count > 1) { return(new Alternates(expressions)); } return(expressions[0]); }
protected override object OnTerminalRangeLoop(INonterminalToken token) { var expression = Visit <IExpression>(token[0]); var range = token.Nonterminal(1).Children; var x1 = Visit <Integer>(range[0]); var rest = ((INonterminalToken)range[1]).Children; if (rest.Count == 0) { return(Repeat.From(expression, x1.Value, x1.Value)); } var x2 = Visit <Integer>(rest[0]); return(Repeat.From(expression, x1.Value, x2.Value)); }
protected override object OnGrammarRules(INonterminalToken token) { return(token.Nonterminal(0).Children.Select(Visit <NonterminalRule>).ToArray()); }