public void RemoveProduction(ProductionModel productionModel) { if (!_matrix.ContainsKey(productionModel.LeftHandSide.NonTerminal)) _matrix.Remove(productionModel.LeftHandSide.NonTerminal); if (!_lookup.ContainsKey(productionModel.LeftHandSide.NonTerminal)) _lookup.Remove(productionModel.LeftHandSide.NonTerminal); }
public void AddProduction(ProductionModel production) { if (!_matrix.ContainsKey(production.LeftHandSide.NonTerminal)) { _matrix[production.LeftHandSide.NonTerminal] = new UniqueList <NonTerminalModel>(); } if (!_lookup.ContainsKey(production.LeftHandSide.NonTerminal)) { _lookup[production.LeftHandSide.NonTerminal] = production; } foreach (var alteration in production.Alterations) { for (var s = 0; s < alteration.Symbols.Count; s++) { var symbol = alteration.Symbols[s]; if (symbol.ModelType != SymbolModelType.Production || symbol.ModelType != SymbolModelType.Reference) { continue; } AddProductionToNewOrExistingSymbolSet(production, symbol); } } }
public void RemoveProduction(ProductionModel productionModel) { if (!_matrix.ContainsKey(productionModel.LeftHandSide.NonTerminal)) { _matrix.Remove(productionModel.LeftHandSide.NonTerminal); } if (!_lookup.ContainsKey(productionModel.LeftHandSide.NonTerminal)) { _lookup.Remove(productionModel.LeftHandSide.NonTerminal); } }
public void GrammarModelConstructorGivenOnlyStartProductionShouldTraverseRecursiveStructureOnlyOnce() { var S = new ProductionModel("S"); var A = new ProductionModel("A"); S.AddWithAnd(S); S.AddWithOr(A); A.AddWithAnd(new LexerRuleModel(new StringLiteralLexerRule("a"))); var grammarModel = new GrammarModel(S); var grammar = grammarModel.ToGrammar(); Assert.AreEqual(3, grammar.Productions.Count); }
IEnumerable<ProductionModel> Expression(EbnfExpression expression, ProductionModel currentProduction) { foreach (var production in Term(expression.Term, currentProduction)) yield return production; if (expression.NodeType != EbnfNodeType.EbnfExpressionAlteration) yield break; var expressionAlteration = expression as EbnfExpressionAlteration; currentProduction.Lambda(); foreach (var production in Expression(expressionAlteration.Expression, currentProduction)) yield return production; }
public IGrammar ToGrammar() { if (StartSymbolExists()) { if (ProductionsAreEmpty()) PopulateMissingProductionsFromStart(Start); AssertStartProductionExistsForStartSymbol(_reachibilityMatrix); } else Start = _reachibilityMatrix.GetStartProduction(); var productions = GetProductionsFromProductionsModel(); var ignoreRules = GetIgnoreRulesFromIgnoreRulesModel(); return new Grammar(Start.LeftHandSide.NonTerminal, productions, ignoreRules); }
public void GrammarModelConstructorGivenOnlyStartProductionShouldDiscoverLinkedProductions() { var S = new ProductionModel("S"); var A = new ProductionModel("A"); var B = new ProductionModel("B"); var C = new ProductionModel("C"); S.AddWithAnd(A); A.AddWithAnd(B); A.AddWithOr(C); B.AddWithAnd(new LexerRuleModel(new StringLiteralLexerRule("b"))); C.AddWithAnd(new LexerRuleModel(new StringLiteralLexerRule("c"))); var grammarModel = new GrammarModel(S); var grammar = grammarModel.ToGrammar(); Assert.AreEqual(5, grammar.Productions.Count); }
private void PopulateMissingProductionsRecursively(ProductionModel production, ISet <INonTerminal> visited) { if (visited.Add(production.LeftHandSide.NonTerminal)) { Productions.Add(production); foreach (var alteration in production.Alterations) { for (var s = 0; s < alteration.Symbols.Count; s++) { var symbol = alteration.Symbols[s]; if (symbol.ModelType == SymbolModelType.Production) { PopulateMissingProductionsRecursively(symbol as ProductionModel, visited); } } } } }
public void AddProduction(ProductionModel production) { if (!_matrix.ContainsKey(production.LeftHandSide.NonTerminal)) _matrix[production.LeftHandSide.NonTerminal] = new UniqueList<NonTerminalModel>(); if (!_lookup.ContainsKey(production.LeftHandSide.NonTerminal)) _lookup[production.LeftHandSide.NonTerminal] = production; foreach (var alteration in production.Alterations) { for(var s = 0; s< alteration.Symbols.Count; s++) { var symbol = alteration.Symbols[s]; if (symbol.ModelType != SymbolModelType.Production || symbol.ModelType != SymbolModelType.Reference) continue; AddProductionToNewOrExistingSymbolSet(production, symbol); } } }
public IGrammar ToGrammar() { if (StartSymbolExists()) { if (ProductionsAreEmpty()) { PopulateMissingProductionsFromStart(Start); } AssertStartProductionExistsForStartSymbol(_reachibilityMatrix); } else { Start = _reachibilityMatrix.GetStartProduction(); } var productions = GetProductionsFromProductionsModel(); var ignoreRules = GetIgnoreRulesFromIgnoreRulesModel(); return(new Grammar(Start.LeftHandSide.NonTerminal, productions, ignoreRules)); }
public void GrammarModelGivenNullStartShouldResolveStartFromProductions() { var S = new ProductionModel("S"); var A = new ProductionModel("A"); var B = new ProductionModel("B"); S.AddWithAnd(A); S.AddWithAnd(B); A.AddWithAnd(new LexerRuleModel(new StringLiteralLexerRule("a"))); A.AddWithAnd(B); B.AddWithAnd(new LexerRuleModel(new StringLiteralLexerRule("b"))); var grammarModel = new GrammarModel(); grammarModel.Productions.Add(S); grammarModel.Productions.Add(A); grammarModel.Productions.Add(B); var grammar = grammarModel.ToGrammar(); Assert.AreEqual(3, grammar.Productions.Count); Assert.IsNotNull(grammar.Start); }
public void AddProduction(ProductionModel production) { if (!this.matrix.ContainsKey(production.LeftHandSide.NonTerminal)) { this.matrix[production.LeftHandSide.NonTerminal] = new UniqueList <NonTerminalModel>(); } if (!this.lookup.ContainsKey(production.LeftHandSide.NonTerminal)) { this.lookup[production.LeftHandSide.NonTerminal] = production; } foreach (var alteration in production.Alterations) { foreach (var symbol in alteration.Symbols) { if (symbol is NonTerminalModel nonTerminalModel) { AddProductionToNewOrExistingSymbolSet(production, nonTerminalModel); } } } }
public ProductionExpression(INonTerminal leftHandSide) { ProductionModel = new ProductionModel(leftHandSide); }
public void OnResetProductions() { Start = null; _reachibilityMatrix.ClearProductions(); }
private void PopulateMissingProductionsRecursively(ProductionModel production, ISet<INonTerminal> visited) { if (visited.Add(production.LeftHandSide.NonTerminal)) { Productions.Add(production); foreach (var alteration in production.Alterations) for (var s =0; s< alteration.Symbols.Count; s++) { var symbol = alteration.Symbols[s]; if (symbol.ModelType == SymbolModelType.Production) PopulateMissingProductionsRecursively(symbol as ProductionModel, visited); } } }
private void OnRemoveProduction(ProductionModel productionModel) { _reachibilityMatrix.RemoveProduction(productionModel); }
private void OnAddProduction(ProductionModel productionModel) { _reachibilityMatrix.AddProduction(productionModel); }
public GrammarModel(ProductionModel start) : this() { Start = start; }
IEnumerable<ProductionModel> Rule(EbnfRule rule) { var nonTerminal = GetNonTerminalFromQualifiedIdentifier(rule.QualifiedIdentifier); var productionModel = new ProductionModel(nonTerminal); foreach(var production in Expression(rule.Expression, productionModel)) yield return production; yield return productionModel; }
IEnumerable<ProductionModel> Repetition(EbnfFactorRepetition repetition, ProductionModel currentProduction) { var name = repetition.ToString(); var nonTerminal = new NonTerminal(name); var repetitionProduction = new ProductionModel(nonTerminal); currentProduction.AddWithAnd(new NonTerminalModel(nonTerminal)); var expression = repetition.Expression; foreach (var production in Expression(expression, repetitionProduction)) yield return production; repetitionProduction.AddWithAnd(new NonTerminalModel(nonTerminal)); repetitionProduction.Lambda(); yield return repetitionProduction; }
IEnumerable<ProductionModel> Optional(EbnfFactorOptional optional, ProductionModel currentProduction) { var name = optional.ToString(); var nonTerminal = new NonTerminal(name); var optionalProduction = new ProductionModel(nonTerminal); currentProduction.AddWithAnd(new NonTerminalModel(nonTerminal)); var expression = optional.Expression; foreach (var production in Expression(expression, optionalProduction)) yield return production; optionalProduction.Lambda(); yield return optionalProduction; }
IEnumerable<ProductionModel> Grouping(EbnfFactorGrouping grouping, ProductionModel currentProduction) { var name = grouping.ToString(); var nonTerminal = new NonTerminal(name); var groupingProduction = new ProductionModel(nonTerminal); currentProduction.AddWithAnd(new NonTerminalModel(nonTerminal)); var expression = grouping.Expression; foreach (var production in Expression(expression, groupingProduction)) yield return production; yield return groupingProduction; }
public void GrammarModelToGrammarShouldCreateGrammar() { var grammarModel = new GrammarModel(); var S = new ProductionModel("S"); var A = new ProductionModel("A"); var B = new ProductionModel("B"); var a = new StringLiteralLexerRule("a"); var b = new StringLiteralLexerRule("b"); var space = new StringLiteralLexerRule(" "); S.AddWithAnd(A.LeftHandSide); S.AddWithAnd(B.LeftHandSide); S.AddWithOr(B.LeftHandSide); A.AddWithAnd(new LexerRuleModel(a)); B.AddWithAnd(new LexerRuleModel(b)); grammarModel.Productions.Add(S); grammarModel.Productions.Add(A); grammarModel.Productions.Add(B); grammarModel.IgnoreRules.Add(new LexerRuleModel(space)); grammarModel.Start = S; var grammar = grammarModel.ToGrammar(); Assert.AreEqual(4, grammar.Productions.Count); Assert.AreEqual(1, grammar.Ignores.Count); }
public void GrammarModelToGrammarShouldAddProductionWhenEmptyDefinition() { var S = new ProductionModel("S"); var grammarModel = new GrammarModel(S); var grammar = grammarModel.ToGrammar(); Assert.AreEqual(1, grammar.Productions.Count); }
public void AddProduction(ProductionModel productionModel) { this.productionModels.Add(productionModel); this.reachabilityMatrix.AddProduction(productionModel); }
IEnumerable<ProductionModel> Term(EbnfTerm term, ProductionModel currentProduction) { foreach (var production in Factor(term.Factor, currentProduction)) yield return production; if (term.NodeType != EbnfNodeType.EbnfTermConcatenation) yield break; var concatenation = term as EbnfTermConcatenation; foreach(var production in Term(concatenation.Term, currentProduction)) yield return production; }
private void OnRemoveProduction(ProductionModel productionModel) { _reachibilityMatrix.RemoveProduction(productionModel); }
public StartProductionSettingModel(ProductionModel productionModel) : base(SettingKey, productionModel.LeftHandSide.NonTerminal.Value) { }
private void OnResetProductions() { Start = null; _reachibilityMatrix.ClearProductions(); }
IEnumerable<ProductionModel> Factor(EbnfFactor factor, ProductionModel currentProduction) { switch (factor.NodeType) { case EbnfNodeType.EbnfFactorGrouping: var grouping = factor as EbnfFactorGrouping; foreach (var production in Grouping(grouping, currentProduction)) yield return production; break; case EbnfNodeType.EbnfFactorOptional: var optional = factor as EbnfFactorOptional; foreach (var production in Optional(optional, currentProduction)) yield return production; break; case EbnfNodeType.EbnfFactorRepetition: var repetition = factor as EbnfFactorRepetition; foreach (var production in Repetition(repetition, currentProduction)) yield return production; break; case EbnfNodeType.EbnfFactorIdentifier: var identifier = factor as EbnfFactorIdentifier; var nonTerminal = GetNonTerminalFromQualifiedIdentifier(identifier.QualifiedIdentifier); currentProduction.AddWithAnd(new NonTerminalModel(nonTerminal)); break; case EbnfNodeType.EbnfFactorLiteral: var literal = factor as EbnfFactorLiteral; var stringLiteralRule = new StringLiteralLexerRule(literal.Value); currentProduction.AddWithAnd( new LexerRuleModel(stringLiteralRule)); break; case EbnfNodeType.EbnfFactorRegex: var regex = factor as EbnfFactorRegex; var nfa = _thompsonConstructionAlgorithm.Transform(regex.Regex); var dfa = _subsetConstructionAlgorithm.Transform(nfa); var dfaLexerRule = new DfaLexerRule(dfa, regex.Regex.ToString()); currentProduction.AddWithAnd(new LexerRuleModel(dfaLexerRule)); break; } }
private void OnAddProduction(ProductionModel productionModel) { _reachibilityMatrix.AddProduction(productionModel); }
private void AddProductionToNewOrExistingSymbolSet(ProductionModel production, NonTerminalModel symbol) { var set = this.matrix.AddOrGetExisting(symbol.NonTerminal); set.AddUnique(production.LeftHandSide); }
private void PopulateMissingProductionsFromStart(ProductionModel start) { var visited = new HashSet<INonTerminal>(); PopulateMissingProductionsRecursively(start, visited); }
private void AddProductionToNewOrExistingSymbolSet(ProductionModel production, SymbolModel symbol) { var set = _matrix.AddOrGetExisting(symbol.Symbol); set.Add(production.LeftHandSide); }
public GrammarModel(ProductionModel start) : this() { Start = start; }
public StartProductionSettingModel(ProductionModel productionModel) : this(productionModel.LeftHandSide.NonTerminal.QualifiedName) { }
private void AddProductionToNewOrExistingSymbolSet(ProductionModel production, SymbolModel symbol) { UniqueList<NonTerminalModel> set = null; if (!_matrix.TryGetValue(symbol.Symbol, out set)) { set = new UniqueList<NonTerminalModel>(); _matrix[symbol.Symbol] = set; } set.Add(production.LeftHandSide); }
private void PopulateMissingProductionsFromStart(ProductionModel start) { var visited = new HashSet <INonTerminal>(); PopulateMissingProductionsRecursively(start, visited); }
public ProductionExpression(FullyQualifiedName fullyQualifiedName) { ProductionModel = new ProductionModel(fullyQualifiedName); }
public void GrammarModelToGrammarShouldResolverProductionReferencesFromOtherGrammars() { var S = new ProductionModel { LeftHandSide = new NonTerminalModel(new FullyQualifiedName("ns1", "S")) }; var A = new ProductionModel { LeftHandSide = new NonTerminalModel(new FullyQualifiedName("ns1", "A")) }; S.Alterations.Add( new AlterationModel( new[] { A })); A.Alterations.Add( new AlterationModel( new[] { new LexerRuleModel( new StringLiteralLexerRule("a"))}) ); var ns1GrammarModel = new GrammarModel { Start = S }; ns1GrammarModel.Productions.Add(S); ns1GrammarModel.Productions.Add(A); var ns1ProductionReferece = new ProductionReferenceModel(ns1GrammarModel.ToGrammar()); var Z = new ProductionModel { LeftHandSide = new NonTerminalModel(new FullyQualifiedName("ns2", "Z")) }; var X = new ProductionModel { LeftHandSide = new NonTerminalModel(new FullyQualifiedName("ns2", "X")) }; X.Alterations.Add( new AlterationModel( new SymbolModel[] { Z, ns1ProductionReferece })); var ns2GrammarModel = new GrammarModel { Start = Z }; ns2GrammarModel.Productions.Add(Z); ns2GrammarModel.Productions.Add(X); var ns2Grammar = ns2GrammarModel.ToGrammar(); Assert.AreEqual(4, ns2Grammar.Productions.Count); }