internal override GrammarExpressionTuple GenerateGrammar(GrammarType grammarType, int grammarNumber, ref int index, ref int additionalGrammarNumber, Action <GrammarPostReport> onIterate, params GrammarExpressionWithOriginal[] dependencies) { CheckDependencies(dependencies); NonTerminalSymbol target = new NonTerminalSymbol(new Label(new SingleLabel(Grammar._DefaultNonTerminalSymbol, index++))); GrammarExpressionTuple grammarExpressionTuple = new GrammarExpressionTuple( this, new Grammar( EnumerateHelper.Sequence( new Rule(EnumerateHelper.Sequence(new Chain(Enumerable.Empty <Grammars.Symbol>())), target) ), target ), grammarNumber ); if (onIterate != null) { onIterate(new GrammarPostReport(grammarExpressionTuple, dependencies)); } return(grammarExpressionTuple); }
protected OptionMatcher(CodeSource codeSource, GrammarType grammarType, Action <ParsingError> errorEvent) { Regex = new Regex($@"({Name})\s*=\s*(\w+);", RegexOptions.IgnoreCase | RegexOptions.Compiled); CodeSource = codeSource ?? throw new ArgumentNullException(nameof(codeSource)); GrammarType = grammarType; ErrorAction = errorEvent ?? throw new ArgumentNullException(nameof(errorEvent)); }
internal override GrammarExpressionTuple GenerateGrammar(GrammarType grammarType, int grammarNumber, ref int index, ref int additionalGrammarNumber, Action <GrammarPostReport> onIterate, params GrammarExpressionWithOriginal[] dependencies) { CheckDependencies(dependencies); Grammar leftExpGrammar = dependencies[0].GrammarExpression.Grammar; Grammar rightExpGrammar = dependencies[1].GrammarExpression.Grammar; NonTerminalSymbol target = new NonTerminalSymbol(new Label(new SingleLabel(Grammar._DefaultNonTerminalSymbol, index++))); Rule rule = new Rule( EnumerateHelper.Sequence( new Chain(EnumerateHelper.Sequence(leftExpGrammar.Target)), new Chain(EnumerateHelper.Sequence(rightExpGrammar.Target)) ), target); IEnumerable <Rule> newRules = leftExpGrammar.Rules.Concat(rightExpGrammar.Rules).Concat(rule); GrammarExpressionTuple grammarExpressionTuple = new GrammarExpressionTuple(this, new Grammar(newRules, target), grammarNumber); if (onIterate != null) { onIterate(new GrammarPostReport(grammarExpressionTuple, dependencies)); } return(grammarExpressionTuple); }
public static BaseParser BuildParser(GrammarType type, string text, List <Message> messages) { var builtGrammar = BuildGrammar(type, text, messages); if (messages.Count(m => m.Type == MessageType.Error) != 0) { return(null); } builtGrammar.RebuildUserificationCache(); BaseTable table = null; switch (type) { case GrammarType.LL: table = new TableLL1(builtGrammar); break; case GrammarType.LR: table = new TableLR1(builtGrammar); break; } messages.AddRange(table.CheckValidity()); table.ExportToCsv(TempName("current_table.csv")); var lexerType = BuildLexer(builtGrammar, "CurrentLexer", messages); if (messages.Count(m => m.Type == MessageType.Error) != 0) { return(null); } /// Создаём парсер BaseParser parser = null; switch (type) { case GrammarType.LL: parser = new Parsing.LL.Parser(builtGrammar, new AntlrLexerAdapter( (Antlr4.Runtime.ICharStream stream) => (Antlr4.Runtime.Lexer)Activator.CreateInstance(lexerType, stream) ) ); break; case GrammarType.LR: parser = new Parsing.LR.Parser(builtGrammar, new AntlrLexerAdapter( (Antlr4.Runtime.ICharStream stream) => (Antlr4.Runtime.Lexer)Activator.CreateInstance(lexerType, stream) ) ); break; } return(parser); }
internal Grammar(GrammarType type) { switch (type) { case GrammarType.Where: SetWhereGrammar(); break; } }
public RegExps.Expression MakeExpression(GrammarType grammarType, Action <Matrix> onBegin = null, Action <Matrix> onIterate = null) { IDictionary <NonTerminalSymbol, int> nonTerminalIndexMap = NonTerminals.Select((s, i) => new KeyValuePair <NonTerminalSymbol, int>(s, i)).ToDictionary(); RegExps.Expression[,] expressions = new RegExps.Expression[nonTerminalIndexMap.Count, nonTerminalIndexMap.Count + 1]; foreach (Rule rule in Rules) { int rowNumber = nonTerminalIndexMap[rule.Target]; foreach (Chain chain in rule.Chains) { SymbolTuple symbolTuple = GetSymbolTuple(grammarType, chain, rule.Target); RegExps.Expression expression; int columnNumber; if (symbolTuple.TerminalSymbol == null) { expression = RegExps.Empty.Instance; } else { expression = new RegExps.Symbol(symbolTuple.TerminalSymbol.Symbol); } if (symbolTuple.NonTerminalSymbol == null) { columnNumber = expressions.GetLength(1) - 1; } else { columnNumber = nonTerminalIndexMap[symbolTuple.NonTerminalSymbol]; } if (expressions[rowNumber, columnNumber] == null) { expressions[rowNumber, columnNumber] = expression; } else { expressions[rowNumber, columnNumber] = new RegExps.BinaryUnion(expression, expressions[rowNumber, columnNumber]); } } for (int i = 0; i < expressions.GetLength(1); i++) { expressions[rowNumber, i] = expressions[rowNumber, i]?.Optimize(); } } Matrix matrix = new Matrix(expressions, nonTerminalIndexMap.OrderBy(kv => kv.Value).Select(kv => kv.Key).ToList().AsReadOnly(), grammarType); return(matrix.Solve(nonTerminalIndexMap[Target], onBegin, onIterate)); }
/// <summary> /// Initializes a new instance of the <see cref="GrammarImpl"/> class. /// </summary> /// <param name="name">The name.</param> /// <param name="type">The type.</param> /// <param name="ambiguityDegree">The ambiguity degree.</param> /// <param name="lookAhead">The look-ahead.</param> /// <param name="encoding">The encoding.</param> /// <param name="definition">The definition.</param> public GrammarImpl(string name, GrammarType type, int? ambiguityDegree, int? lookAhead, IEncoding encoding, IGrammarDefinition definition) : base(name, type, ambiguityDegree, lookAhead, encoding) { #region Contract Contract.Requires<ArgumentNullException>(name != null); Contract.Requires<InvalidEnumArgumentException>(Enum.IsDefined(typeof(GrammarType), type)); Contract.Requires<ArgumentNullException>(encoding != null); #endregion this.definition = definition; }
// Los clientes solicitan la gramática que necesitan public void SetGrammar(GrammarType grammarType) { speechRecognizer.UnloadAllGrammars(); switch (grammarType) { case GrammarType.MOUSE_VOICE: speechRecognizer.LoadGrammar(GetMouseAndVoiceGrammar()); break; case GrammarType.ONLY_VOICE: speechRecognizer.LoadGrammar(GetOnlyVoiceGrammar()); break; } }
internal override GrammarExpressionTuple GenerateGrammar(GrammarType grammarType, int grammarNumber, ref int index, ref int additionalGrammarNumber, Action <GrammarPostReport> onIterate, params GrammarExpressionWithOriginal[] dependencies) { CheckDependencies(dependencies); if (IterationCount == 0) { return(Empty.Instance.GenerateGrammar(grammarType, grammarNumber, ref index, ref additionalGrammarNumber, onIterate)); } GrammarExpressionTuple original = dependencies[0].GrammarExpression; if (IterationCount == 1) { GrammarExpressionTuple grammarExpressionTuple = new GrammarExpressionTuple(this, original.Grammar, grammarNumber); if (onIterate != null) { onIterate(new GrammarPostReport(grammarExpressionTuple, new GrammarExpressionWithOriginal(original).AsSequence())); } return(grammarExpressionTuple); } GrammarExpressionTuple dependency1 = original; for (int i = 1; i < IterationCount; i++) { GrammarExpressionTuple dependency2 = Mirror(original, ref index, ref additionalGrammarNumber); int number; if (i == IterationCount - 1) { number = grammarNumber; } else { number = additionalGrammarNumber++; } GrammarExpressionTuple expressionTuple = new BinaryConcat(dependency1.Expression, dependency2.Expression). GenerateGrammar(grammarType, number, ref index, ref additionalGrammarNumber, onIterate, new GrammarExpressionWithOriginal(dependency1), new GrammarExpressionWithOriginal(dependency2, original)); dependency1 = expressionTuple; } return(dependency1); }
public Grammar MakeGrammar(GrammarType grammarType) { switch (grammarType) { case GrammarType.Left: return(MakeGrammarLeft()); case GrammarType.Right: return(MakeGrammarRight()); default: throw new InvalidOperationException(Grammar.GrammarIsNotSupportedMessage); } }
private static IEnumerable <T> Enumerate <T>(GrammarType grammarType, params T[] items) { switch (grammarType) { case GrammarType.Left: return(EnumerateHelper.Sequence(items)); case GrammarType.Right: return(EnumerateHelper.ReverseSequence(items)); default: throw new NotSupportedException(string.Format(GrammarIsNotSupportedMessage, grammarType)); } }
private static IEnumerable <T> Enumerate <T>(IReadOnlyList <T> list, GrammarType grammarType) { switch (grammarType) { case GrammarType.Left: return(list); case GrammarType.Right: return(list.FastReverse()); default: throw new NotSupportedException(string.Format(GrammarIsNotSupportedMessage, grammarType)); } }
public override void EnterGrammarType([NotNull] ANTLRv4Parser.GrammarTypeContext context) { if (context.GetChild(0).GetText() == "parser") { Type = GrammarType.Parser; } else if (context.GetChild(0).GetText() == "lexer") { Type = GrammarType.Lexer; } else { Type = GrammarType.Combined; } }
internal override GrammarExpressionTuple GenerateGrammar(GrammarType grammarType, int grammarNumber, ref int index, ref int additionalGrammarNumber, Action <GrammarPostReport> onIterate, params GrammarExpressionWithOriginal[] dependencies) { CheckDependencies(dependencies); switch (grammarType) { case GrammarType.Left: return(GenerateLeftGrammar(grammarNumber, ref index, ref additionalGrammarNumber, onIterate, dependencies)); case GrammarType.Right: return(GenerateRightGrammar(grammarNumber, ref index, ref additionalGrammarNumber, onIterate, dependencies)); default: throw new InvalidOperationException(UnknownGrammarMessage(grammarType)); } }
/// <summary> /// 递归处理数据 /// </summary> /// <param name="all">语法类型原始数据</param> /// <param name="node">要递归的起始节点</param> /// <param name="root">根节点</param> public void DGNode(List <GrammarType> all, GrammarType node, TreeView root) { var childs = all.Where(o => o.ParentID == node.ID).ToList(); if (childs.Count > 0) { root.nodes = new List <TreeView>(); } foreach (var item in childs) { var temp = new TreeView(); temp.id = item.ID; temp.text = item.TypeName; temp.tags = item; root.nodes.Add(temp); DGNode(all, item, temp); } }
public Grammar MakeGrammar(GrammarType grammarType, Action <GrammarPostReport> onIterate = null) { IReadOnlyList <DependencyCollection> dependencyMap = DependencyMap; GrammarExpressionWithOriginal[] grammars = new GrammarExpressionWithOriginal[dependencyMap.Count]; int index = _StartIndex; int additionalGrammarNumber = _StartIndex + dependencyMap.Count + 1; for (int i = 0; i < dependencyMap.Count; i++) { Expression expression = dependencyMap[i].Expression; IEnumerable <GrammarExpressionWithOriginal> dependencies = dependencyMap[i].Select(item => grammars[item]); grammars[i] = new GrammarExpressionWithOriginal(expression.GenerateGrammar(grammarType, _StartIndex + i, ref index, ref additionalGrammarNumber, onIterate, dependencies.ToArray())); } return(grammars[grammars.Length - 1].GrammarExpression.Grammar); }
/// <summary> /// 保存语法类型 /// </summary> /// <returns></returns> public string SaveGMType() { try { var id = context.Request["ID"]; GrammarType obj; if (string.IsNullOrEmpty(id)) { obj = new GrammarType(); obj.ID = Guid.NewGuid().ToString(); obj.LoginName = context.Session["LoginName"].ToString(); obj.CreateUser = context.Session["LoginName"].ToString(); obj.CreateDate = DateTime.Now; obj.UpdateUser = context.Session["LoginName"].ToString(); obj.UpdateDate = DateTime.Now; } else { obj = gmBll.GetModel(id); obj.UpdateUser = context.Session["LoginName"].ToString(); obj.UpdateDate = DateTime.Now; } obj.ParentID = context.Request["ParentID"]; obj.TypeName = context.Request["TypeName"]; obj.Remark = context.Request["Remark"]; obj.Seq = int.Parse(context.Request["Seq"]); if (string.IsNullOrEmpty(id)) { gmBll.Add(obj); } else { gmBll.Update(obj); } dataGridModel.Msg = "保存成功!"; dataGridModel.Code = CodeEnum.Success; return(JsonData.GetResult(dataGridModel)); } catch (Exception ex) { throw ex; } }
private Color GetColorByGrammerType(GrammarType type) { switch (type) { case GrammarType.Class: { return Colors.Red; } case GrammarType.Function: { return Colors.Blue; } case GrammarType.Keyword: { return Colors.BurlyWood; } } return Colors.Black; }
private static Grammar CreateDefaultAndFill(GrammarType type, string lexerText, string parserText, string grammarName, string directory) { var result = new Grammar { Name = grammarName, Type = type, }; result.Directory = directory; InitFiles(result); foreach (string file in result.Files) { string text = file.Contains(Grammar.LexerPostfix) ? lexerText : parserText; File.WriteAllText(Path.Combine(directory, file), text); } return(result); }
public Matrix(Expression[,] matrix, IEnumerable <NonTerminalSymbol> nonTerminals, GrammarType grammarType) { if (matrix == null) { throw new ArgumentNullException(nameof(matrix)); } if (nonTerminals == null) { throw new ArgumentNullException(nameof(nonTerminals)); } if (nonTerminals.AnyNull()) { throw new ArgumentException("Collection contains null elements.", nameof(nonTerminals)); } _NonTerminals = nonTerminals.ToArray(); _Matrix = matrix; GrammarType = grammarType; if (_NonTerminals.Length != _Matrix.GetLength(0) || _NonTerminals.Length != _Matrix.GetLength(1) - 1) { throw new InvalidOperationException("Non terminals collection length is not equal either matrix rows count or matrix column count minus 1."); } for (int i = 0; i < _Matrix.GetLength(0); i++) { bool allNulls = true; for (int j = 0; j < _Matrix.GetLength(1); j++) { allNulls &= _Matrix[i, j] == null; } if (allNulls) { throw new InvalidOperationException("At least one equation has no elements."); } } NonTerminals = _NonTerminals.AsReadOnly(); }
private Color GetColorByGrammerType(GrammarType type) { switch (type) { case GrammarType.Class: { return(Colors.Red); } case GrammarType.Function: { return(Colors.Blue); } case GrammarType.Keyword: { return(Colors.BurlyWood); } } return(Colors.Black); }
public static Grammar BuildGrammar(GrammarType type, string text, List <Message> log) { var scanner = new SpecParsing.Scanner(); scanner.SetSource(text, 0); var specParser = new SpecParsing.Parser(scanner); specParser.ConstructedGrammar = new Grammar(type); var success = specParser.Parse(); log.AddRange(specParser.Log); log.AddRange(scanner.Log); if (!success) { return(null); } return(specParser.ConstructedGrammar); }
private string TypeName(GrammarType type) { var value = TypeName(type.Symbol); if (type.IsRef) { value = "ref " + value; if (type.IsOptional) { value = "[DisallowNull] " + value; } } if (type.IsOptional) { value += "?"; } if (type.IsList) { value = $"{grammar.ListType}<{value}>"; } return(value); }
public Grammar MakeStateMachineGrammar(GrammarType grammarType, Action <IReadOnlySet <Rule> > onBegin = null, Action <MakeStateMachineGrammarPostReport> onIterate = null) { ISet <Rule> newRules = new HashSet <Rule>(); if (onBegin != null) { onBegin(newRules.AsReadOnly()); } ISet <NonTerminalSymbol> newNonTerminals = new HashSet <NonTerminalSymbol>(NonTerminals); foreach (Rule rule in Rules) { foreach (Chain chain in rule.Chains) { ISet <Rule> newChainRules = new HashSet <Rule>(); int state = 0; NonTerminalSymbol nonTerminalSymbol = null; Symbol otherSymbol = null; Chain newChain; foreach (Symbol symbol in Enumerate(chain, grammarType)) { switch (state) { case 0: nonTerminalSymbol = symbol.As <NonTerminalSymbol>(); otherSymbol = symbol; if (nonTerminalSymbol != null) { state = 1; } else { state = 2; } break; case 1: otherSymbol = symbol; state = 3; break; case 2: newChain = new Chain(otherSymbol.AsSequence()); newNonTerminals.Add(nonTerminalSymbol = GetNewNonTerminal(newNonTerminals)); newChainRules.Add(new Rule(newChain.AsSequence(), nonTerminalSymbol)); otherSymbol = symbol; state = 3; break; default: newChain = new Chain(Enumerate(grammarType, nonTerminalSymbol, otherSymbol)); newNonTerminals.Add(nonTerminalSymbol = GetNewNonTerminal(newNonTerminals)); newChainRules.Add(new Rule(newChain.AsSequence(), nonTerminalSymbol)); otherSymbol = symbol; break; } } switch (state) { case 0: newChain = Chain.Empty; break; case 1: case 2: newChain = new Chain(otherSymbol.AsSequence()); break; default: newChain = new Chain(Enumerate(grammarType, nonTerminalSymbol, otherSymbol)); break; } newChainRules.Add(new Rule(newChain.AsSequence(), rule.Target)); newChainRules = Normalize(newChainRules); ISet <Rule> previousRules = newRules.ToHashSet(); newRules.AddRange(newChainRules); if (onIterate != null) { onIterate(new MakeStateMachineGrammarPostReport(rule.Target, chain, previousRules, newChainRules, newRules, newChainRules.Count > 1)); } } } return(new Grammar(newRules, Target)); }
public StateMachine MakeStateMachine(GrammarType grammarType) { ISet <Transition> transitions = new HashSet <Transition>(); ISet <Label> finalStates = new HashSet <Label>(); NonTerminalSymbol additionalState = GetNewNonTerminal(NonTerminals); switch (grammarType) { case GrammarType.Left: finalStates.Add(Target.Label); break; case GrammarType.Right: finalStates.Add(additionalState.Label); break; default: throw new NotSupportedException(string.Format(GrammarIsNotSupportedMessage, grammarType)); } foreach (Rule rule in Rules) { foreach (Chain chain in rule.Chains) { SymbolTuple symbolTuple = GetSymbolTuple(grammarType, chain, rule.Target, additionalState); if (symbolTuple.NonTerminalSymbol == null && symbolTuple.NonTerminalSymbol == null) { switch (grammarType) { case GrammarType.Left: finalStates.Add(additionalState.Label); break; case GrammarType.Right: finalStates.Add(Target.Label); break; default: throw new NotSupportedException(string.Format(GrammarIsNotSupportedMessage, grammarType)); } } else { Label currentState; Label nextState; switch (grammarType) { case GrammarType.Left: currentState = symbolTuple.NonTerminalSymbol.Label; nextState = symbolTuple.Target.Label; break; case GrammarType.Right: currentState = symbolTuple.Target.Label; nextState = symbolTuple.NonTerminalSymbol.Label; break; default: throw new NotSupportedException(string.Format(GrammarIsNotSupportedMessage, grammarType)); } transitions.Add(new Transition(currentState, symbolTuple.TerminalSymbol.Symbol, nextState)); } } } StateMachine stateMachine; switch (grammarType) { case GrammarType.Left: stateMachine = new StateMachine(additionalState.Label, finalStates, transitions); break; case GrammarType.Right: stateMachine = new StateMachine(Target.Label, finalStates, transitions); break; default: throw new NotSupportedException(string.Format(GrammarIsNotSupportedMessage, grammarType)); } return(stateMachine); }
private SymbolTuple GetSymbolTuple(GrammarType grammarType, Chain chain, NonTerminalSymbol ruleTarget, NonTerminalSymbol finalTarget = null) { int state = 0; Symbol otherSymbol = null; NonTerminalSymbol nonTerminalSymbol = null; TerminalSymbol terminalSymbol = null; foreach (Symbol symbol in Enumerate(chain, grammarType)) { switch (state) { case 0: otherSymbol = symbol; state = 1; break; case 1: nonTerminalSymbol = otherSymbol.As <NonTerminalSymbol>(); if (nonTerminalSymbol == null) { throw new InvalidOperationException("Expected non terminal symbol."); } terminalSymbol = symbol.As <TerminalSymbol>(); if (terminalSymbol == null) { throw new InvalidOperationException("Expected terminal symbol."); } state = 2; break; default: throw new InvalidOperationException("Expected 2 or less symbols."); } } switch (state) { case 0: if (ruleTarget != Target && finalTarget != null) { throw new InvalidOperationException("Expected that rule target equals to grammar target."); } return(new SymbolTuple(ruleTarget, null, null)); case 1: nonTerminalSymbol = finalTarget != null ? finalTarget : otherSymbol.As <NonTerminalSymbol>(); terminalSymbol = otherSymbol.As <TerminalSymbol>(); if (terminalSymbol == null && nonTerminalSymbol == null) { throw new InvalidOperationException(string.Format("The symbol type is not supported symbol type: {0}.", otherSymbol)); } if (finalTarget != null && terminalSymbol == null) { throw new InvalidOperationException("One symbol chain must contain terminal symbol."); } goto case 2; case 2: default: return(new SymbolTuple(ruleTarget, terminalSymbol, nonTerminalSymbol)); } }
public GrammarProperty(string name, GrammarType type) { Name = name; Type = type; }
/// <summary> /// Генерация библиотеки с парсером /// </summary> /// <param name="type">Тип парсера (LL или LR)</param> /// <param name="text">Грамматика разбираемого формата файлов</param> /// <param name="namespace">Пространство имён для сгенерированного парсера</param> /// <param name="path">Путь к каталогу, в котором необходимо сгенерировать библиотеку</param> /// <param name="messages">Лог генерации парсера</param> /// <returns>Признак успешности выполнения операции</returns> public static bool GenerateLibrary( GrammarType type, string text, string @namespace, string path, string keyPath, List <Message> messages ) { const string ANTLR_LIBRARY = "Antlr4.Runtime.Standard.dll"; /// Строим объект грамматики и проверяем, корректно ли прошло построение var builtGrammar = BuildGrammar(type, text, messages); /// Проверяем, не появились ли ошибки после построения грамматики if (messages.Count(m => m.Type == MessageType.Error) != 0) { return(false); } builtGrammar.RebuildUserificationCache(); /// Строим таблицу и проверяем, соответствует ли она указанному типу грамматики BaseTable table = null; switch (type) { case GrammarType.LL: table = new TableLL1(builtGrammar); break; case GrammarType.LR: table = new TableLR1(builtGrammar); break; } messages.AddRange(table.CheckValidity()); /// Проверяем, не появились ли ошибки после построения таблицы if (messages.Count(m => m.Type == MessageType.Error) != 0) { return(false); } var lexerFileName = TempName($"{@namespace.Replace('.', '_')}_Lexer.cs"); var parserFileName = TempName($"{@namespace.Replace('.', '_')}_Parser.cs"); var grammarFileName = TempName($"{@namespace.Replace('.', '_')}_Grammar.cs"); var nodeGeneratorFileName = TempName($"{@namespace.Replace('.', '_')}_NodeGenerator.cs"); BuildLexer(builtGrammar, Path.GetFileNameWithoutExtension(lexerFileName), messages); /// Проверяем, не появились ли ошибки после генерации исходников лексера if (messages.Count(m => m.Type == MessageType.Error) != 0) { return(false); } File.WriteAllText(grammarFileName, GetGrammarProviderText(builtGrammar, @namespace)); File.WriteAllText(parserFileName, GetParserProviderText(@namespace)); File.WriteAllText(nodeGeneratorFileName, GetNodeGeneratorText(builtGrammar, @namespace)); if (!String.IsNullOrEmpty(keyPath) && !File.Exists(keyPath)) { /// Создаём файл ключа Process process = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "cmd.exe", Arguments = $"/C chcp 1251 | \"Resources/sn.exe\" -k \"{keyPath}\"", CreateNoWindow = true, RedirectStandardOutput = true, UseShellExecute = false }; process.StartInfo = startInfo; process.Start(); process.WaitForExit(); } /// Компилируем библиотеку var codeProvider = new CSharpCodeProvider();; var compilerParams = new System.CodeDom.Compiler.CompilerParameters(); compilerParams.GenerateInMemory = false; compilerParams.OutputAssembly = Path.Combine(path, $"{@namespace}.dll"); compilerParams.ReferencedAssemblies.Add(ANTLR_LIBRARY); compilerParams.ReferencedAssemblies.Add("Land.Core.dll"); compilerParams.ReferencedAssemblies.Add("System.dll"); compilerParams.ReferencedAssemblies.Add("System.Core.dll"); compilerParams.ReferencedAssemblies.Add("mscorlib.dll"); if (!String.IsNullOrEmpty(keyPath)) { compilerParams.CompilerOptions = $"/keyfile:\"{keyPath}\""; } var compilationResult = codeProvider.CompileAssemblyFromFile(compilerParams, lexerFileName, grammarFileName, parserFileName, nodeGeneratorFileName); if (compilationResult.Errors.Count == 0) { File.Copy(ANTLR_LIBRARY, Path.Combine(path, ANTLR_LIBRARY), true); return(true); } else { foreach (System.CodeDom.Compiler.CompilerError error in compilationResult.Errors) { if (error.IsWarning) { messages.Add(Message.Warning( $"Предупреждение: {error.FileName}; ({error.Line}, {error.Column}); {error.ErrorText}", null, "C# Compiler" )); } else { messages.Add(Message.Error( $"Ошибка: {error.FileName}; ({error.Line}, {error.Column}); {error.ErrorText}", null, "C# Compiler" )); } } return(messages.All(m => m.Type != MessageType.Error)); } }
public void Highlight(int line, int start, int length, GrammarType type) { //this.mTextBox //richTextBox1.SelectionColor = Color.Red; }
private List <LabelInfo> FindLabelsInScope(SnapshotPoint triggerPoint) { List <LabelInfo> labels = new List <LabelInfo>(); /* use the experimental model to locate and process the expression */ Stopwatch stopwatch = Stopwatch.StartNew(); // lex the entire document var currentSnapshot = triggerPoint.Snapshot; var input = new SnapshotCharStream(currentSnapshot, new Span(0, currentSnapshot.Length)); var lexer = new ANTLRLexer(input); var tokens = new CommonTokenStream(lexer); tokens.Fill(); // locate the last token before the trigger point while (true) { IToken nextToken = tokens.Lt(1); if (nextToken.Type == CharStreamConstants.EndOfFile) { break; } if (nextToken.StartIndex > triggerPoint.Position) { break; } tokens.Consume(); } bool inAction = false; IToken triggerToken = tokens.LT(-1); switch (triggerToken.Type) { case ANTLRLexer.RULE_REF: case ANTLRLexer.TOKEN_REF: case ANTLRLexer.DOLLAR: break; case ANTLRLexer.ACTION: case ANTLRLexer.FORCED_ACTION: case ANTLRLexer.SEMPRED: case ANTLRLexer.ARG_ACTION: inAction = true; break; default: return(labels); } NetworkInterpreter interpreter = CreateNetworkInterpreter(tokens); while (interpreter.TryStepBackward()) { if (interpreter.Contexts.Count == 0 || interpreter.Contexts.Count > 4000) { break; } if (interpreter.Contexts.All(context => context.BoundedStart)) { break; } } if (interpreter.Failed) { interpreter.Contexts.Clear(); } interpreter.CombineBoundedStartContexts(); HashSet <IToken> labelTokens = new HashSet <IToken>(TokenIndexEqualityComparer.Default); foreach (var context in interpreter.Contexts) { var tokenTransitions = context.Transitions.Where(i => i.TokenIndex != null).ToList(); for (int i = 1; i < tokenTransitions.Count - 1; i++) { if (tokenTransitions[i].Symbol != ANTLRLexer.TOKEN_REF && tokenTransitions[i].Symbol != ANTLRLexer.RULE_REF) { continue; } // we add explicit labels, plus implicit labels if we're in an action if (tokenTransitions[i + 1].Symbol == ANTLRLexer.ASSIGN || tokenTransitions[i + 1].Symbol == ANTLRLexer.PLUS_ASSIGN) { RuleBinding rule = interpreter.Network.StateRules[tokenTransitions[i + 1].Transition.SourceState.Id]; if (rule.Name == AntlrAtnBuilder.RuleNames.TreeRoot || rule.Name == AntlrAtnBuilder.RuleNames.ElementNoOptionSpec) { labelTokens.Add(tokenTransitions[i].Token); } } else if (inAction && tokenTransitions[i - 1].Symbol != ANTLRLexer.ASSIGN && tokenTransitions[i - 1].Symbol != ANTLRLexer.PLUS_ASSIGN) { RuleBinding rule = interpreter.Network.StateRules[tokenTransitions[i].Transition.SourceState.Id]; if (rule.Name == AntlrAtnBuilder.RuleNames.Terminal || rule.Name == AntlrAtnBuilder.RuleNames.NotTerminal || rule.Name == AntlrAtnBuilder.RuleNames.RuleRef) { labelTokens.Add(tokenTransitions[i].Token); } } } } foreach (var token in labelTokens) { labels.Add(new LabelInfo(token.Text, "(label) " + token.Text, new SnapshotSpan(triggerPoint.Snapshot, Span.FromBounds(token.StartIndex, token.StopIndex + 1)), StandardGlyphGroup.GlyphGroupField, Enumerable.Empty <LabelInfo>())); } /* add scopes */ if (inAction) { /* add global scopes */ IList <IToken> tokensList = tokens.GetTokens(); for (int i = 0; i < tokensList.Count - 1; i++) { var token = tokensList[i]; /* all global scopes appear before the first rule. before the first rule, the only place a ':' can appear is * in the form '::' for things like @lexer::namespace{} */ if (token.Type == ANTLRLexer.COLON && tokensList[i + 1].Type == ANTLRLexer.COLON) { break; } if (token.Type == ANTLRLexer.SCOPE) { var nextToken = tokensList.Skip(i + 1).FirstOrDefault(t => t.Channel == TokenChannels.Default); if (nextToken != null && (nextToken.Type == ANTLRLexer.RULE_REF || nextToken.Type == ANTLRLexer.TOKEN_REF)) { // TODO: parse scope members IToken actionToken = tokensList.Skip(nextToken.TokenIndex + 1).FirstOrDefault(t => t.Channel == TokenChannels.Default); IEnumerable <LabelInfo> members = Enumerable.Empty <LabelInfo>(); if (actionToken != null && actionToken.Type == ANTLRLexer.ACTION) { IEnumerable <IToken> scopeMembers = ExtractScopeAttributes(nextToken); members = scopeMembers.Select(member => { string name = member.Text; SnapshotSpan definition = new SnapshotSpan(triggerPoint.Snapshot, Span.FromBounds(member.StartIndex, member.StopIndex + 1)); StandardGlyphGroup glyph = StandardGlyphGroup.GlyphGroupField; IEnumerable <LabelInfo> nestedMembers = Enumerable.Empty <LabelInfo>(); return(new LabelInfo(name, string.Empty, definition, glyph, nestedMembers)); }); } labels.Add(new LabelInfo(nextToken.Text, "(global scope) " + nextToken.Text, new SnapshotSpan(triggerPoint.Snapshot, Span.FromBounds(nextToken.StartIndex, nextToken.StopIndex + 1)), StandardGlyphGroup.GlyphGroupNamespace, members)); } } } /* add rule scopes */ // todo } /* add arguments and return values */ if (inAction) { HashSet <IToken> argumentTokens = new HashSet <IToken>(TokenIndexEqualityComparer.Default); foreach (var context in interpreter.Contexts) { var tokenTransitions = context.Transitions.Where(i => i.TokenIndex != null).ToList(); for (int i = 1; i < tokenTransitions.Count; i++) { if (tokenTransitions[i].Symbol == ANTLRLexer.RETURNS || tokenTransitions[i].Symbol == ANTLRLexer.COLON) { break; } if (tokenTransitions[i].Symbol == ANTLRLexer.ARG_ACTION) { argumentTokens.Add(tokenTransitions[i].Token); } } } foreach (var token in argumentTokens) { IEnumerable <IToken> arguments = ExtractArguments(token); foreach (var argument in arguments) { labels.Add(new LabelInfo(argument.Text, "(parameter) " + argument.Text, new SnapshotSpan(triggerPoint.Snapshot, Span.FromBounds(argument.StartIndex, argument.StopIndex + 1)), StandardGlyphGroup.GlyphGroupVariable, Enumerable.Empty <LabelInfo>())); } } } /* add return values */ if (inAction) { HashSet <IToken> returnTokens = new HashSet <IToken>(TokenIndexEqualityComparer.Default); foreach (var context in interpreter.Contexts) { var tokenTransitions = context.Transitions.Where(i => i.TokenIndex != null).ToList(); for (int i = 1; i < tokenTransitions.Count - 1; i++) { if (tokenTransitions[i].Symbol == ANTLRLexer.COLON) { break; } if (tokenTransitions[i].Symbol == ANTLRLexer.RETURNS) { if (tokenTransitions[i + 1].Symbol == ANTLRLexer.ARG_ACTION) { returnTokens.Add(tokenTransitions[i + 1].Token); } break; } } } foreach (var token in returnTokens) { IEnumerable <IToken> returnValues = ExtractArguments(token); foreach (var returnValue in returnValues) { labels.Add(new LabelInfo(returnValue.Text, "(return value) " + returnValue.Text, new SnapshotSpan(triggerPoint.Snapshot, Span.FromBounds(returnValue.StartIndex, returnValue.StopIndex + 1)), StandardGlyphGroup.GlyphGroupVariable, Enumerable.Empty <LabelInfo>())); } } } /* add intrinsic labels ($start, $type, $text, $enclosingRuleName) */ IToken ruleNameToken = null; HashSet <IToken> enclosingRuleNameTokens = new HashSet <IToken>(TokenIndexEqualityComparer.Default); foreach (var context in interpreter.Contexts) { var tokenTransitions = context.Transitions.Where(i => i.Symbol == ANTLRLexer.RULE_REF || i.Symbol == ANTLRLexer.TOKEN_REF).ToList(); if (!tokenTransitions.Any()) { continue; } ruleNameToken = tokenTransitions.First().Token; if (ruleNameToken != null) { enclosingRuleNameTokens.Add(ruleNameToken); } } foreach (var token in enclosingRuleNameTokens) { // TODO: add members labels.Add(new LabelInfo(token.Text, "(enclosing rule) " + token.Text, new SnapshotSpan(triggerPoint.Snapshot, Span.FromBounds(token.StartIndex, token.StopIndex + 1)), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); } GrammarType grammarType = GrammarType.None; int mark = tokens.Mark(); try { tokens.Seek(0); bool hasGrammarType = false; while (!hasGrammarType) { int la1 = tokens.LA(1); switch (la1) { case ANTLRLexer.GRAMMAR: IToken previous = tokens.LT(-1); if (previous == null) { grammarType = GrammarType.Combined; } else if (previous.Type == ANTLRLexer.LEXER) { grammarType = GrammarType.Lexer; } else if (previous.Type == ANTLRLexer.PARSER) { grammarType = GrammarType.Parser; } else if (previous.Type == ANTLRLexer.TREE) { grammarType = GrammarType.TreeParser; } else { grammarType = GrammarType.None; } hasGrammarType = true; break; case CharStreamConstants.EndOfFile: hasGrammarType = true; break; default: break; } tokens.Consume(); } } finally { tokens.Rewind(mark); } if (inAction) { switch (grammarType) { case GrammarType.Combined: if (ruleNameToken == null) { goto default; } if (ruleNameToken.Type == ANTLRLexer.RULE_REF) { goto case GrammarType.Parser; } else { goto case GrammarType.Lexer; } case GrammarType.Lexer: labels.Add(new LabelInfo("text", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("type", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("line", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("index", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("pos", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("channel", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("start", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("stop", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("int", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); break; case GrammarType.Parser: labels.Add(new LabelInfo("text", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("start", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("stop", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("tree", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("st", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); break; case GrammarType.TreeParser: labels.Add(new LabelInfo("text", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("start", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("tree", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("st", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); break; default: // if we're unsure about the grammar type, include all the possible options to make sure we're covered labels.Add(new LabelInfo("text", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("type", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("line", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("index", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("pos", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("channel", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("start", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("stop", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("int", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("tree", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); labels.Add(new LabelInfo("st", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>())); break; } } return(labels); }
public ListenerOptionMatcher(CodeSource codeSource, GrammarType grammarType, Action <ParsingError> errorEvent) : base(codeSource, grammarType, errorEvent) { }
public CaseInsensitiveTypeOptionMatcher(CodeSource codeSource, GrammarType grammarType, Action <ParsingError> errorEvent) : base(codeSource, grammarType, errorEvent) { }
/// <summary> /// Initializes a new instance of the <see cref="Grammar"/> class. /// </summary> /// <param name="name">The name.</param> /// <param name="type">The type.</param> /// <param name="ambiguityDegree">The ambiguity degree.</param> /// <param name="lookAhead">The look-ahead.</param> /// <param name="encoding">The encoding.</param> protected Grammar(string name, GrammarType type, int? ambiguityDegree, int? lookAhead, IEncoding encoding) { #region Contract Contract.Requires<ArgumentNullException>(name != null); Contract.Requires<InvalidEnumArgumentException>(Enum.IsDefined(typeof(GrammarType), type)); Contract.Requires<ArgumentNullException>(encoding != null); #endregion this.Name = name; this.Type = type; this.AmbiguityDegree = ambiguityDegree; this.LookAhead = lookAhead; this.Encoding = encoding; }
public RootOptionMatcher(CodeSource codeSource, GrammarType grammarType, Action <ParsingError> errorEvent, List <string> existingRules) : base(codeSource, grammarType, errorEvent) { ExistingRules = existingRules; }
/** Given a grammar type, what should be the default action scope? * If I say @members in a COMBINED grammar, for example, the * default scope should be "parser". */ public virtual string GetDefaultActionScope( GrammarType grammarType ) { switch ( grammarType ) { case GrammarType.Lexer: return "lexer"; case GrammarType.Parser: case GrammarType.Combined: return "parser"; case GrammarType.TreeParser: return "treeparser"; } return null; }