public JSParser() { try { string input = ""; StringBuilder text = new StringBuilder(); Console.Console.Instance.WriteLine("Input the chat."); AntlrInputStream inputStream = new AntlrInputStream(text.ToString()); JavaScriptLexer speakLexer = new JavaScriptLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(speakLexer); JavaScriptParser speakParser = new JavaScriptParser(commonTokenStream); //SpeakParser.ChatContext chatContext = speakParser.chat(); //JavaScriptParserVisitor visitor = new JavaScriptParserVisitor(); //visitor.Visit(chatContext); //foreach (var line in visitor.Lines) //{ // Console.Console.Instance.WriteLine("{0} has said {1}", line.Person, line.Text); //} //JavaScriptParser parser = new JavaScriptParser(); //parser. } catch (Exception ex) { Console.Console.Instance.WriteLine("Error: " + ex); } }
protected override Lexer InitLexer(ICharStream inputStream) { var lexer = new JavaScriptLexer(inputStream); if (JavaScriptType != JavaScriptType.Undefined) { lexer.UseStrictDefault = JavaScriptType == JavaScriptType.Strict; } return(lexer); }
public override void Parse(FileInfo file) { Lexer lexer = new JavaScriptLexer(CharStreams.fromPath(file.FullName)); CommonTokenStream tokens = new CommonTokenStream(lexer); JavaScriptParser parser = new JavaScriptParser(tokens); ParserRuleContext context = parser.program(); var children = context.children; foreach (IParseTree child in children) { this.ParseNode(child); } }
public static IParseTree Parse(string input) { var str = new AntlrInputStream(input); var lexer = new JavaScriptLexer(str); Lexer = lexer; var tokens = new CommonTokenStream(lexer); TokenStream = tokens; var parser = new JavaScriptParser(tokens); Parser = parser; var tree = parser.program(); Input = lexer.InputStream.ToString(); TokenStream = parser.TokenStream; Tree = tree; return(tree); }
internal static CompiledScript Compile(string script, bool isGlobal) { CommonTokenStream tokens = new CommonTokenStream(); JavaScriptLexer lexer = new JavaScriptLexer(new ANTLRStringStream(script)); tokens.TokenSource = lexer; ScriptSource Source = new ScriptSource(script); JavaScriptParser parser = new JavaScriptParser(tokens); TopStatementList r = (TopStatementList) parser.program().Tree; r.ConstructionComplete(); if (JSContext.DebugOutput) { DumpTree(r, 0); } if (JSContext.OutputAssembly && isGlobal) { GenerateExternalAssembly(r); r.Reset(); } return GenerateMethod(r, Source); }
protected override ITreeNode[] CreateWhitespaces(IEnumerable <string> wsTexts) { if (wsTexts == null) { throw new ArgumentNullException("wsTexts"); } return(wsTexts.Where(text => !text.IsEmpty()).Select(text => { if (text.IsNewLine()) { return CreateNewLine(); } // consistency check (remove in release?) if (!JavaScriptLexer.IsWhitespace(text)) { throw new ApplicationException("Inconsistent space structure"); } return CreateSpace(text); }).ToArray()); }
internal static CompiledScript Compile(string script, bool isGlobal) { CommonTokenStream tokens = new CommonTokenStream(); JavaScriptLexer lexer = new JavaScriptLexer(new ANTLRStringStream(script)); tokens.TokenSource = lexer; ScriptSource Source = new ScriptSource(script); JavaScriptParser parser = new JavaScriptParser(tokens); TopStatementList r = (TopStatementList)parser.program().Tree; r.ConstructionComplete(); if (JSContext.DebugOutput) { DumpTree(r, 0); } if (JSContext.OutputAssembly && isGlobal) { GenerateExternalAssembly(r); r.Reset(); } return(GenerateMethod(r, Source)); }
static void Main(string[] args) { bool show_tree = false; bool show_tokens = false; bool old = false; bool two_byte = false; string file_name = null; string input = null; System.Text.Encoding encoding = null; for (int i = 0; i < args.Length; ++i) { if (args[i].Equals("-tokens")) { show_tokens = true; continue; } else if (args[i].Equals("-two-byte")) { two_byte = true; continue; } else if (args[i].Equals("-old")) { old = true; continue; } else if (args[i].Equals("-tree")) { show_tree = true; continue; } else if (args[i].Equals("-input")) { input = args[++i]; } else if (args[i].Equals("-file")) { file_name = args[++i]; } else if (args[i].Equals("-encoding")) { ++i; encoding = Encoding.GetEncoding( args[i], new EncoderReplacementFallback("(unknown)"), new DecoderReplacementFallback("(error)")); if (encoding == null) { throw new Exception(@"Unknown encoding. Must be an Internet Assigned Numbers Authority (IANA) code page name. https://www.iana.org/assignments/character-sets/character-sets.xhtml"); } } } ICharStream str = null; if (input == null && file_name == null) { str = CharStreams.fromStream(System.Console.OpenStandardInput()); } else if (input != null) { str = CharStreams.fromString(input); } else if (file_name != null) { if (two_byte) { str = new TwoByteCharStream(file_name); } else if (old) { FileStream fs = new FileStream(file_name, FileMode.Open); str = new Antlr4.Runtime.AntlrInputStream(fs); } else if (encoding == null) { str = CharStreams.fromPath(file_name); } else { str = CharStreams.fromPath(file_name, encoding); } } var lexer = new JavaScriptLexer(str); if (show_tokens) { StringBuilder new_s = new StringBuilder(); for (int i = 0; ; ++i) { var ro_token = lexer.NextToken(); var token = (CommonToken)ro_token; token.TokenIndex = i; new_s.AppendLine(token.ToString()); if (token.Type == Antlr4.Runtime.TokenConstants.EOF) { break; } } System.Console.Error.WriteLine(new_s.ToString()); lexer.Reset(); } var tokens = new CommonTokenStream(lexer); var parser = new JavaScriptParser(tokens); var listener_lexer = new ErrorListener <int>(); var listener_parser = new ErrorListener <IToken>(); lexer.AddErrorListener(listener_lexer); parser.AddErrorListener(listener_parser); DateTime before = DateTime.Now; var tree = parser.program(); DateTime after = DateTime.Now; System.Console.Error.WriteLine("Time: " + (after - before)); if (listener_lexer.had_error || listener_parser.had_error) { System.Console.Error.WriteLine("Parse failed."); } else { System.Console.Error.WriteLine("Parse succeeded."); } if (show_tree) { System.Console.Error.WriteLine(tree.ToStringTree(parser)); } System.Environment.Exit(listener_lexer.had_error || listener_parser.had_error ? 1 : 0); }