public GrammarData(Grammar grammar) { if (grammar == null) throw new ArgumentNullException(nameof(grammar)); Grammar = grammar; AugmentedRoots = new List<GrammarDefinition>(); _allElements = new HashSet<GrammarElement>(); GrammarReductionsMapping = new Dictionary<GrammarDefinition, List<GrammarReduction>>(); AllReductions = new List<GrammarReduction>(); foreach (var rootDefinition in grammar.RootDefinitions) { var augmentedRoot = new GrammarDefinition(rootDefinition.Name + "'", rootDefinition + Grammar.Eof); if (rootDefinition == grammar.DefaultRoot) DefaultAugmentedRoot = augmentedRoot; AugmentedRoots.Add(augmentedRoot); AddReductions(augmentedRoot); } if(DefaultAugmentedRoot==null) throw new InvalidOperationException("Default root of grammar is not present in the root definitions collection."); AllElements = new ReadOnlyCollection<GrammarElement>(_allElements.ToArray()); }
public ParserContext(AutomatonSourceParser parser, Grammar grammar, Lexer lexer) { Parser = parser; Grammar = grammar; Lexer = lexer; ParserStack = new Stack<ParserNode>(); SyntaxErrors = new List<SyntaxError>(); }
private static void TestSerializer(Grammar grammar) { var data = new GrammarData(grammar); var serializer = new ParserAutomatonSerializer(data); var result = GrammarCompiler.Compile(data); using (var stream = new MemoryStream()) { serializer.Serialize(stream, result.Automaton); stream.Position = 0; var newAutomaton = serializer.Deserialize(stream); Assert.AreEqual(result.Automaton.DefaultInitialState.Id, newAutomaton.DefaultInitialState.Id); Assert.AreEqual(result.Automaton.States.Count, newAutomaton.States.Count); for (int index = 0; index < result.Automaton.States.Count; index++) { var expectedState = result.Automaton.States[index]; var actualState = newAutomaton.States[index]; ValidateState(expectedState, actualState); } } }
public ParserAutomaton(Grammar grammar) { Grammar = grammar; States = new List<ParserState>(); InitialStates = new Dictionary<GrammarDefinition, ParserState>(); }
public AutomatonSourceParser(Grammar grammar) : this(GrammarCompiler.Compile(new GrammarData(grammar)).Automaton) { }
public static GrammarCompilationResult Compile(Grammar grammar) { return Compile(new GrammarData(grammar)); }
public DeserializerContext(Grammar grammar, BinaryReader reader) { Reader = reader; Automaton = new ParserAutomaton(grammar); }