protected virtual void InitStartingTokensForRule(Dictionary <string, NonTerminal <IN> > nonTerminals, Rule <IN> rule) { if (rule.PossibleLeadingTokens == null || rule.PossibleLeadingTokens.Count == 0) { rule.PossibleLeadingTokens = new List <IN>(); if (rule.Clauses.Count > 0) { IClause <IN> first = rule.Clauses[0]; if (first is TerminalClause <IN> ) { TerminalClause <IN> term = first as TerminalClause <IN>; rule.PossibleLeadingTokens.Add(term.ExpectedToken); rule.PossibleLeadingTokens = rule.PossibleLeadingTokens.Distinct <IN>().ToList <IN>(); } else { NonTerminalClause <IN> nonterm = first as NonTerminalClause <IN>; InitStartingTokensForNonTerminal(nonTerminals, nonterm.NonTerminalName); if (nonTerminals.ContainsKey(nonterm.NonTerminalName)) { NonTerminal <IN> firstNonTerminal = nonTerminals[nonterm.NonTerminalName]; firstNonTerminal.Rules.ForEach(r => { rule.PossibleLeadingTokens.AddRange(r.PossibleLeadingTokens); }); rule.PossibleLeadingTokens = rule.PossibleLeadingTokens.Distinct <IN>().ToList <IN>(); } } } } }
protected override void InitStartingTokensForRule(Dictionary <string, NonTerminal <IN> > nonTerminals, Rule <IN> rule) { if (rule.PossibleLeadingTokens == null || rule.PossibleLeadingTokens.Count == 0) { rule.PossibleLeadingTokens = new List <IN>(); if (rule.Clauses.Count > 0) { IClause <IN> first = rule.Clauses[0]; if (first is TerminalClause <IN> ) { TerminalClause <IN> term = first as TerminalClause <IN>; InitStartingTokensWithTerminal(rule, term); } else if (first is NonTerminalClause <IN> ) { NonTerminalClause <IN> nonterm = first as NonTerminalClause <IN>; InitStartingTokensWithNonTerminal(rule, nonterm, nonTerminals); } else if (first is ZeroOrMoreClause <IN> ) { ZeroOrMoreClause <IN> many = first as ZeroOrMoreClause <IN>; InitStartingTokensWithZeroOrMore(rule, many, nonTerminals); } else if (first is OneOrMoreClause <IN> ) { OneOrMoreClause <IN> many = first as OneOrMoreClause <IN>; InitStartingTokensWithOneOrMore(rule, many, nonTerminals); } } } }
private Rule <IN> BuildNonTerminal(Tuple <string, string> ntAndRule) { var rule = new Rule <IN>(); var clauses = new List <IClause <IN> >(); var ruleString = ntAndRule.Item2; var clausesString = ruleString.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); foreach (var item in clausesString) { IClause <IN> clause = null; var isTerminal = false; var token = default(IN); try { var tIn = typeof(IN); var b = Enum.TryParse(item, out token); if (b) { isTerminal = true; ; } //token = (IN)Enum.Parse(tIn , item); //isTerminal = true; } catch (ArgumentException) { isTerminal = false; } if (isTerminal) { clause = new TerminalClause <IN>(token); } else if (item == "[d]") { if (clauses.Last() is TerminalClause <IN> discardedTerminal) { discardedTerminal.Discarded = true; } } else { clause = new NonTerminalClause <IN>(item); } if (clause != null) { clauses.Add(clause); } } rule.Clauses = clauses; //rule.Key = ntAndRule.Item1 + "_" + ntAndRule.Item2.Replace(" ", "_"); return(rule); }
public SyntaxParseResult <IN> ParseTerminal(IList <Token <IN> > tokens, TerminalClause <IN> term, int position) { SyntaxParseResult <IN> result = new SyntaxParseResult <IN>(); result.IsError = !term.Check(tokens[position].TokenID); result.EndingPosition = !result.IsError ? position + 1 : position; Token <IN> token = tokens[position]; token.Discarded = term.Discarded; result.Root = new SyntaxLeaf <IN>(token); return(result); }
private void InitStartingTokensWithOneOrMore(Rule <IN> rule, OneOrMoreClause <IN> manyClause, Dictionary <string, NonTerminal <IN> > nonTerminals) { if (manyClause.Clause is TerminalClause <IN> ) { TerminalClause <IN> term = manyClause.Clause as TerminalClause <IN>; InitStartingTokensWithTerminal(rule, term); } else if (manyClause.Clause is NonTerminalClause <IN> ) { NonTerminalClause <IN> nonterm = manyClause.Clause as NonTerminalClause <IN>; InitStartingTokensWithNonTerminal(rule, nonterm, nonTerminals); } }
private IClause <IN> BuildTerminalOrNonTerimal(string name, bool discard = false) { IN token = default(IN); IClause <IN> clause; bool isTerminal = false; bool b = Enum.TryParse <IN>(name, out token); if (b) { isTerminal = true; ; } if (isTerminal) { clause = new TerminalClause <IN>(token, discard); } else { clause = new NonTerminalClause <IN>(name); } return(clause); }
private void InitStartingTokensWithTerminal(Rule <IN> rule, TerminalClause <IN> term) { rule.PossibleLeadingTokens.Add(term.ExpectedToken); rule.PossibleLeadingTokens = rule.PossibleLeadingTokens.Distinct().ToList(); }