private static void Main(string[] args) { try { string input = ""; StringBuilder text = new StringBuilder(); Console.WriteLine("Input the chat."); // to type the EOF character and end the input: use CTRL+D, then press <enter> while ((input = Console.ReadLine()) != "\u0004") { text.AppendLine(input); } AntlrInputStream inputStream = new AntlrInputStream(text.ToString()); SpeakLexer speakLexer = new SpeakLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(speakLexer); SpeakParser speakParser = new SpeakParser(commonTokenStream); SpeakParser.ChatContext chatContext = speakParser.chat(); SpeakVisitor visitor = new SpeakVisitor(); visitor.Visit(chatContext); foreach (var line in visitor.Lines) { Console.WriteLine("{0} has said {1}", line.Person, line.Text); } } catch (Exception ex) { Console.WriteLine("Error: " + ex); } }
public void TestChat() { SpeakParser parser = Setup("john says \"hello\" \n michael says \"world\" \n"); SpeakParser.ChatContext context = parser.chat(); SpeakVisitor visitor = new SpeakVisitor(); visitor.Visit(context); Assert.AreEqual(2, visitor.Lines.Count); }
static void Main(string[] args) { AntlrInputStream inputStream = new AntlrInputStream(text.ToString()); Hemo speakLexer = new Hemo(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(speakLexer); SpeakParser speakParser = new SpeakParser(commonTokenStream); SpeakParser.ChatContext chatContext = speakParser.chat(); SpeakVisitor visitor = new SpeakVisitor(); visitor.Visit(chatContext); }
/// <summary> /// Visit a parse tree produced by <see cref="SpeakParser.chat"/>. /// <para> /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/> /// on <paramref name="context"/>. /// </para> /// </summary> /// <param name="context">The parse tree.</param> /// <return>The visitor result.</return> public virtual Result VisitChat([NotNull] SpeakParser.ChatContext context) { return(VisitChildren(context)); }
/// <summary> /// Exit a parse tree produced by <see cref="SpeakParser.chat"/>. /// <para>The default implementation does nothing.</para> /// </summary> /// <param name="context">The parse tree.</param> public virtual void ExitChat([NotNull] SpeakParser.ChatContext context) { }
public override void ExitChat([NotNull] SpeakParser.ChatContext context) { line += "ExitChat: " + context.GetText() + "\r\n"; }