public void ParseObjectiveC() { var inputStream = new AntlrInputStream( @"@interface MyObject : NSObject { int val; id obj; } + (void)classMethod:(id)arg; - (id)method:(NSObject*)arg1 with:(int)arg2; @end"); var lexer = new ObjectiveCLexer(inputStream); var commonTokenStream = new CommonTokenStream(lexer); var parser = new ObjectiveCParser(commonTokenStream); var visitor = new CstBuilderForAntlr4(parser); visitor.Visit(parser.translationUnit()); Console.WriteLine(visitor.FinishParsing()); }
private void Process(string input) { if (string.IsNullOrWhiteSpace(input)) { return; } var sw = Stopwatch.StartNew(); var lexer = new ObjectiveCLexer(new AntlrInputStream(File.ReadAllText(input))); var tokenStream = new CommonTokenStream(lexer); tokenStream.Fill(); var errorHandler = new AntlrErrorStrategy(); var parser = new ObjectiveCParser(tokenStream) { BuildParseTree = true, ErrorHandler = errorHandler }; ParserRuleContext tree = parser.translationUnit(); var tokenSymbolicNames = (string[])lexer.GetType().GetField("_SymbolicNames", BindingFlags.Static | BindingFlags.NonPublic).GetValue(lexer); var tokens = GetTokens(tokenStream, tokenSymbolicNames); var errorStatus = errorHandler.HasErrors ? $"Parser Error at {string.Join(Environment.NewLine, errorHandler.OffendingTokens.Select(t => TokenToString(t, tokenSymbolicNames)))}" : "No Errors"; ErrorStatus.Text = $"Elapsed: {sw.Elapsed.TotalSeconds}s" + Environment.NewLine + errorStatus; var count = GetTreeChildCount(tree); var criteria = count <= 500; Tree = criteria ? tree : ParserRuleContext.EmptyContext; Parser = parser; LexerSymbolicNames = tokenSymbolicNames; LexerTokens = tokens; }