private static string[] GenerateTokenNames(IVocabulary vocabulary, int length) { string[] tokenNames = new string[length]; for (int i = 0; i < tokenNames.Length; i++) { tokenNames[i] = vocabulary.GetLiteralName(i); if (tokenNames[i] == null) { tokenNames[i] = vocabulary.GetSymbolicName(i); } if (tokenNames[i] == null) { tokenNames[i] = "<INVALID>"; } } return tokenNames; }
protected virtual IDictionary <string, int> CreateTokenTypeMap(IVocabulary vocabulary) { var result = new Dictionary <string, int>(); for (int i = 0; i <= Atn.maxTokenType; i++) { string literalName = vocabulary.GetLiteralName(i); if (literalName != null) { result[literalName] = i; } string symbolicName = vocabulary.GetSymbolicName(i); if (symbolicName != null) { result[symbolicName] = i; } } result["EOF"] = TokenConstants.EOF; return(result); }
private string RenderToken(IToken token, bool showChannel, IVocabulary vocabulary) { string symbolicName = vocabulary?.GetSymbolicName(token.Type) ?? token.Type.ToString(); string value = TokenValueDisplayMode == TokenValueDisplayMode.Trim ? token.Text?.Trim() : token.Text; string tokenValue = string.Empty; if (TokenValueDisplayMode != TokenValueDisplayMode.Ignore && value != null && symbolicName != value) { tokenValue = value.Length <= MaxTokenValueLength ? value : value.Substring(0, MaxTokenValueLength) + "..."; } string channelValue = string.Empty; if (showChannel) { channelValue = "c: " + token.Channel.ToString(); // TODO: channel name instead of identifier. } string result = symbolicName; if (!string.IsNullOrEmpty(tokenValue) || !string.IsNullOrEmpty(channelValue)) { var strings = new List <string>(); if (!string.IsNullOrEmpty(tokenValue)) { strings.Add(tokenValue); } if (!string.IsNullOrEmpty(channelValue)) { strings.Add(channelValue); } result = $"{result} ({(string.Join(", ", strings))})"; } return(result); }
public void SyntaxError([NotNull] IRecognizer recognizer, [Nullable] IToken offendingSymbol, int line, int charPositionInLine, [NotNull] string msg, [Nullable] RecognitionException e) => AddParseError(line, charPositionInLine, offendingSymbol == null ? msg : String.Format("Unexpected token {0} ({1})!", offendingSymbol.Text, vocabulary.GetSymbolicName(offendingSymbol.Type)));
/// <summary> /// Generate Template of javascript code for parse tree data. /// </summary> /// <returns>Template of javascript code for parse tree with token names and rule names</returns> public StringBuilder JsParseTeeFileTemplate() { StringBuilder template = new StringBuilder(); string code = ""; StringBuilder sb = new StringBuilder(); // Token names IVocabulary vocabulary = Lexer.Vocabulary; for (int i = 0; i <= vocabulary.MaxTokenType; i++) { if (i < vocabulary.MaxTokenType) { sb.AppendFormat("{0}\"{1}\",{2}", "\t", vocabulary.GetSymbolicName(i), "\n"); } else { sb.AppendFormat("\t\"{0}\"", vocabulary.GetSymbolicName(i)); } } String tokenNames = sb.ToString(); // Rule names sb = new StringBuilder(); for (int i = 0; i < Parser.RuleNames.Length; i++) { if (i < Parser.RuleNames.Length - 1) { sb.AppendFormat("{0}\"{1}\",{2}", "\t", Parser.RuleNames[i], "\n"); } else { sb.AppendFormat("\t\"{0}\"", Parser.RuleNames[i]); } } String ruleNames = sb.ToString(); code = @"(function(global, factory){{{{ ""use strict""; factory(global); }}}})(window, function(global){{{{ ""use strict""; var tokenNames = [ {0} ]; var ruleNames = [ {1} ]; var tokens = []; var rules = []; var errors = []; var tree; {{0}} {{1}} {{2}} tree = {{3}}; var ruleListeners = []; var everyListners = []; var CST = {{{{ tokenNames: function(n){{{{ return tokenNames[n]; }}}}, ruleNames: function(n){{{{ return ruleNames[n]; }}}}, tokens: function(){{{{ return tokens; }}}}, rules: function(){{{{ return rules; }}}}, errors: function(){{{{ return errors; }}}}, tree: function(){{{{ return tree; }}}}, addRuleListener: function(fn, ruleIndex){{{{ if(!ruleIndex){{{{ everyListners.push(fn); }}}} else{{{{ if(ruleListeners[ruleIndex] === undefined) ruleListeners[ruleIndex] = []; ruleListeners[ruleIndex].push(fn); }}}} }}}}, clearRuleListeners: function(){{{{ ruleListeners = []; everyListners = []; }}}}, walk: function(nodes){{{{ if(!nodes){{{{ nodes = [{{{{r: 0, c: tree}}}}]; return CST.walk(nodes); }}}} for(var i=0; i<nodes.length; i++){{{{ var isRule = nodes[i].hasOwnProperty('r'); if(isRule){{{{ var listeners = ruleListeners[nodes[i].r]; if(listeners !== undefined) listeners.forEach(function(el, i, arr){{{{el(nodes[i])}}}}); if(everyListners.length > 0) everyListners.forEach(function(el){{{{el(nodes[i])}}}}); CST.walk(nodes[i].c); }}}} }}}} }}}} }}}}; global.CST = CST; }}}}); "; template.AppendFormat(code, tokenNames, ruleNames); return(template); }