public static void Main(string[] args) { if (args.Length > 0) { string inputFileName = args[0]; if (!Path.IsPathRooted(inputFileName)) { inputFileName = Path.Combine(Environment.CurrentDirectory, inputFileName); } ICharStream input = new ANTLRFileStream(inputFileName); CLexer lex = new CLexer(input); CommonTokenStream tokens = new CommonTokenStream(lex); CParser parser = new CParser(tokens); try { parser.translation_unit(); } catch (RecognitionException re) { Console.Out.WriteLine(re.StackTrace); } } else { Console.Error.WriteLine("Usage: cparse <input-file>"); } }
public void Chl_Address_Attribute() { var file = Given_File( @"[[reko::address(""0123:4567"")]] int foo(char * bar); "); var sc = new ServiceContainer(); var arch = new Mock <IProcessorArchitecture>(); var platform = new Mock <IPlatform>(); arch.Setup(a => a.MemoryGranularity).Returns(8); platform.Setup(p => p.PointerType).Returns(PrimitiveType.Ptr32); platform.Setup(p => p.GetBitSizeFromCBasicType(CBasicType.Int)).Returns(32); platform.Setup(p => p.Architecture).Returns(arch.Object); platform.Setup(p => p.CreateCParser(It.IsAny <TextReader>(), It.IsAny <ParserState>())) .Returns(new Func <TextReader, ParserState, CParser>((r, s) => { var lex = new CLexer(r, CLexer.StdKeywords); return(new CParser(s ?? new ParserState(), lex)); })); var addr = Address.SegPtr(0123, 4567); platform.Setup(p => p.TryParseAddress("0123:4567", out addr)).Returns(true); var chl = new CHeaderLoader(sc, ImageLocation.FromUri("file:foo.inc"), file); var typelib = chl.Load(platform.Object, new TypeLibrary()); Assert.IsTrue(typelib.Procedures.TryGetValue(addr, out var proc)); Assert.AreEqual(proc.Name, "foo"); Assert.AreEqual(proc.Signature.ToString(), "(fn void ())"); }
public void Dfa2_UserDefinedStackArgs() { var arch = new X86ArchitectureFlat32(new ServiceContainer(), "x86-protected-32", new Dictionary <string, object>()); var pb = new ProgramBuilder(arch); var test = pb.Add( new UserProcedure(pb.NextAddress(), "test") { CSignature = "void test(int a, int b)" }, m => { var r1 = m.Reg32("r1", 1); var r2 = m.Reg32("r2", 2); var fp = m.Frame.FramePointer; m.Assign(r1, m.Mem32(m.IAdd(fp, 4))); m.Assign(r2, m.Mem32(m.IAdd(fp, 8))); m.Assign(r1, m.IAdd(r1, r2)); m.MStore(m.Ptr32(0x010008), r1); m.Return(); }); var program = pb.BuildProgram(); var platform = new Mock <IPlatform>(); platform.Setup(p => p.Architecture).Returns(arch); platform.Setup(p => p.IsImplicitArgumentRegister(It.IsAny <RegisterStorage>())) .Returns(false); platform.Setup(p => p.DefaultCallingConvention).Returns("__cdecl"); platform.Setup(p => p.GetCallingConvention(null)) .Returns(new X86CallingConvention(4, 4, 4, true, false)); platform.Setup(p => p.GetBitSizeFromCBasicType(CBasicType.Int)).Returns(32); platform.Setup(p => p.PointerType).Returns(PrimitiveType.Ptr32); platform.Setup(p => p.CreateCParser(It.IsAny <TextReader>(), It.IsAny <ParserState>())) .Returns(new Func <TextReader, ParserState, CParser>((r, s) => { var lex = new CLexer(r, CLexer.MsvcKeywords); return(new CParser(s ?? new ParserState(), lex)); })); var dynamicLinker = new Mock <IDynamicLinker>().Object; program.Platform = platform.Object; var usb = new UserSignatureBuilder(program); usb.BuildSignatures(new FakeDecompilerEventListener()); var dfa = new DataFlowAnalysis(program, dynamicLinker, sc); dfa.AnalyzeProgram(); var sExp = @"// test // Return size: 4 void test(int32 a, int32 b) test_entry: // succ: l1 l1: Mem6[0x00010008<p32>:word32] = a + b return // succ: test_exit test_exit: "; AssertProgram(sExp, pb.Program); }
public ProcedureBase_v1 ParseFunctionDeclaration(string fnDecl) { try { var lexer = new CLexer(new StringReader(fnDecl + ";")); var symbols = program.CreateSymbolTable(); var oldProcs = symbols.Procedures.Count; var cstate = new ParserState(symbols.NamedTypes.Keys); var cParser = new CParser(cstate, lexer); var decl = cParser.Parse_ExternalDecl(); if (decl == null) { return(null); } //$HACK: Relying on a side effect here to // get both the procedure name and the signature. Ew. symbols.AddDeclaration(decl); if (symbols.Procedures.Count == oldProcs) { return(null); } return(symbols.Procedures.Last()); } catch (Exception ex) { //$TODO: if user has supplied a signature that can't parse, // we must notify them in the diagnostics window with a // WARNING. Debug.Print("{0}\r\n{1}", ex.Message, ex.StackTrace); return(null); } }
public void Convert() { var lexer = new CLexer(rdr); var parser = new CParser(parserState, lexer); var declarations = parser.Parse(); var symbolTable = new SymbolTable(platform) { NamedTypes = { { "off_t", new PrimitiveType_v1 { Domain = Domain.SignedInt, ByteSize = 4 } }, //$BUGBUG: arch-dependent! { "ssize_t", new PrimitiveType_v1 { Domain = Domain.SignedInt, ByteSize = 4 } }, //$BUGBUG: arch-dependent! { "size_t", new PrimitiveType_v1 { Domain = Domain.UnsignedInt, ByteSize = 4 } }, //$BUGBUG: arch-dependent! { "va_list", new PrimitiveType_v1 { Domain = Domain.Pointer, ByteSize = platform.PointerType.Size } } } }; foreach (var decl in declarations) { symbolTable.AddDeclaration(decl); } var lib = new SerializedLibrary { Types = symbolTable.Types.ToArray(), Procedures = symbolTable.Procedures.ToList(), }; var ser = SerializedLibrary.CreateSerializer(); ser.Serialize(writer, lib); }
public void displayProgram(string program) { double currY = 10; this.drawingCanvas.Children.Clear(); this.drawingCanvas.Height = 0; this.drawingCanvas.Width = 0; this.deltaWidth = 0; this.deltaHeight = 0; this.moveX = 0; try { CToken[] tokens = CLexer.parseTokens(program); foreach (CGrammar.CFunctionDefinition matchedFunction in CParser.CGrammar.CParser.getFunctionContents(tokens)) { drawGraph(matchedFunction, 0, currY); currY += matchedFunction.height + 60; } } catch (System.ArgumentException exc) { System.Windows.MessageBox.Show("There is an error in your source code: " + exc.Message); } catch (System.InvalidOperationException exc) { System.Windows.MessageBox.Show("There is an error in your source code: " + exc.Message); } catch { System.Windows.MessageBox.Show("An unexpected error occured!"); } }
public void Compile() { using (var fileStream = new StreamReader(_stream)) { var inputStream = new AntlrInputStream(fileStream); var lexer = new CLexer(inputStream); var tokenStream = new CommonTokenStream(lexer); var parser = new CParser(tokenStream); var compilationUnit = parser.compilationUnit(); if (parser.NumberOfSyntaxErrors == 0 && compilationUnit != null) { var preBuilder = new CPreBuilder( _fileName, compilationUnit); var cilCodeGenerator = new CILCodeGenerator(preBuilder); cilCodeGenerator.Generate(); if (File.Exists(preBuilder.ProgramFileName)) { OutputFileName = preBuilder.ProgramFileName; } } } }
public GlobalDataItem_v2?ParseGlobalDeclaration(string txtGlobal) { try { var lexer = new CLexer(new StringReader(txtGlobal + ";"), CLexer.GccKeywords); //$REVIEW: what's the right thing? var symbols = program.CreateSymbolTable(); var oldVars = symbols.Variables.Count; var cstate = new ParserState(symbols); var cParser = new CParser(cstate, lexer); var decl = cParser.Parse_ExternalDecl(); if (decl == null) { return(null); } //$HACK: Relying on a side effect here to // get both the global type. Ew. symbols.AddDeclaration(decl); if (symbols.Variables.Count == oldVars) { return(null); } return(symbols.Variables.Last()); } catch (Exception ex) { Debug.Print("{0}\r\n{1}", ex.Message, ex.StackTrace); return(null); } }
public override CParser CreateCParser(TextReader rdr, ParserState?state) { state ??= new ParserState(); var lexer = new CLexer(rdr, CLexer.MsvcKeywords); var parser = new CParser(state, lexer); return(parser); }
private CommonTokenStream GetCommonTokenStream(AntlrInputStream inputStream) { CLexer lexer = new CLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); commonTokenStream.Fill(); return(commonTokenStream); }
private void Given_Parser(string cText) { var state = new ParserState(); var lexer = new CLexer(new StringReader(cText), CLexer.MsvcKeywords); var cparser = new CParser(state, lexer); var attr = cparser.Parse_AttributeSpecifier(); this.sap = new ServiceAttributeParser(attr); }
public CILexer(IBuffer buffer) { Buffer = buffer; ICharStream iCharStream = new AntlrInputStream(buffer.GetText()); antlrLexer = new CLexer(iCharStream); IList <IToken> tokens = antlrLexer.GetAllTokens(); CurrentPosition = 0; }
public void TestHasMoreTokensReturnsValueFromTokenizer() { var m = new Mock <ITokenizer <TokenType> >(); m.SetupGet(t => t.HasMoreTokens).Returns(true); var l = new CLexer(m.Object); Assert.That(l.HasMoreTokens, Is.True); }
public void TestNextTokenReturnsValidToken() { var m = new Mock <ITokenizer <TokenType> >(); m.Setup(t => t.GetNextToken(It.IsAny <bool>())).Returns(new Token <TokenType>()); var l = new CLexer(m.Object); var token = l.GetNextToken(); Assert.That(token, Is.Not.Null); }
public void TestSkipToNextLine() { var m = new Mock <ITokenizer <TokenType> >(); m.Setup(t => t.SkipToNextLine()); var l = new CLexer(m.Object); l.SkipToNextLine(); m.Verify(t => t.SkipToNextLine()); }
public CToken(string tokenCode, int lineIndex = -1, int startSymIndex = -1) { this.tokenCode = tokenCode; if (CLexer.tokenExists(tokenCode)) { this.tokenType = CLexer.getTokenType(tokenCode); } this.lineNumber = lineIndex; this.startSym = startSymIndex; }
public void TestPushToken() { var token = CToken.Create(new Token <TokenType>()); var m = new Mock <ITokenizer <TokenType> >(); var l = new CLexer(m.Object); l.PushToken(token); var otherToken = l.GetNextToken(); Assert.That(otherToken, Is.EqualTo(token)); }
public void ParseC() { var inputStream = new AntlrInputStream(@" int main(int argc, char **args) { printf(args[0]); return 0; }"); var lexer = new CLexer(inputStream); var commonTokenStream = new CommonTokenStream(lexer); var parser = new CParser(commonTokenStream); var visitor = new CstBuilderForAntlr4(parser); visitor.Visit(parser.compilationUnit()); Console.WriteLine(visitor.FinishParsing()); }
public CParser CreateParser(string code) { ICharStream stream = CharStreams.fromstring(code); var lexer = new CLexer(stream); lexer.AddErrorListener(new ThrowExceptionErrorListener()); ITokenStream tokens = new CommonTokenStream(lexer); var parser = new CParser(tokens); parser.BuildParseTree = true; parser.RemoveErrorListeners(); parser.AddErrorListener(new ThrowExceptionErrorListener()); return(parser); }
public override TypeLibrary Load(IPlatform platform, TypeLibrary dstLib) { var rdr = new StreamReader(new MemoryStream(bytes)); var lexer = new CLexer(rdr); var state = new ParserState(); var parser = new CParser(state, lexer); var symbolTable = new SymbolTable(platform); var declarations = parser.Parse(); var tldser = new TypeLibraryDeserializer(platform, true, dstLib); foreach (var decl in declarations) { ProcessDeclaration(decl, platform, tldser, symbolTable); } return(dstLib); }
public void Setup() { this.mockFactory = new CommonMockFactory(); var platform = mockFactory.CreateMockPlatform(); platform.Setup(p => p.CreateCParser(It.IsAny <TextReader>(), It.IsAny <ParserState>())) .Returns(new Func <TextReader, ParserState, CParser>((r, s) => { var lex = new CLexer(r, CLexer.MsvcKeywords); return(new CParser(s ?? new ParserState(), lex)); })); this.program = new Program { Platform = platform.Object, Architecture = platform.Object.Architecture, }; }
public override void Parse(FileInfo file) { Lexer lexer = new CLexer(CharStreams.fromPath(file.FullName)); CommonTokenStream tokens = new CommonTokenStream(lexer); CParser parser = new CParser(tokens); ParserRuleContext context = parser.compilationUnit(); var children = context.children; foreach (IParseTree child in children) { this.ParseNode(child); } }
public void TextEquals(string beforePP, string afterPP) { var expectLexer = new CLexer(afterPP); var pp = new CPreProcessor(new CLexer(beforePP)) .DefineCounter(); while (true) { var expected = expectLexer.Next(); var got = pp.Next(); Assert.Equal(expected.Kind, got.Kind); Assert.Equal(expected.LogicalText, got.LogicalText); if (expected.Kind == CTokenType.End) { break; } } }
static void Main(string[] args) { var sourceCode = @" #define FOO(x, y) x ## y FOO(L, ""asd"") "; var lexer = new CLexer(sourceCode); var pp = new CPreProcessor(lexer); while (true) { var token = pp.Next(); if (token.Kind == CTokenType.End) { break; } Console.WriteLine(token.LogicalText); } }
public string Tokenize(string fileContent) { AntlrInputStream inputStream = new AntlrInputStream(fileContent); CLexer lexer = new CLexer(inputStream); Antlr4.Runtime.CommonTokenStream tokenStream = new Antlr4.Runtime.CommonTokenStream(lexer); tokenStream.Fill(); string result = ""; IList <Antlr4.Runtime.IToken> tokens = tokenStream.GetTokens(); foreach (var token in tokens) { result += (token.Type == CLexer.Identifier) ? "ID" : token.Text; } result = result.Replace("<EOF>", ""); return(result); }
private static void Main(string[] args) { var lex = new CLexer(new AntlrInputStream(Input)); var tokens = new CommonTokenStream(lex); var par = new CParser(tokens); var ctx = par.namespaceDeclaration(); var child = ctx.GetChild <CParser.ClassDeclarationContext>(0); var walker = new ParseTreeWalker(); walker.Walk(new ParseTreeListenerEmpty(), ctx); var c = par.compileUnit(); var s = c.ToStringTree(); /* * MyPascalParser.myprogram_return X = g.myprogram(); * Console.WriteLine(X.Tree); // Writes: nill * Console.WriteLine(X.Start); // Writes: [@0,0:4='begin',<4>,1:0] * Console.WriteLine(X.Stop); // Writes: [@35,57:57='end',<19>,12:2] */ }
public void MyParseMethod() { MyCListener myCListener = new MyCListener(); String input = "int x = 4;"; AntlrInputStream stream = new AntlrInputStream(input); ITokenSource lexer = new CLexer(stream); ITokenStream tokens = new CommonTokenStream(lexer); CParser parser = new CParser(tokens); parser.BuildParseTree = true; //IParseTree tree = parser.primaryExpression(); IParseTree tree = parser.compilationUnit(); //Console.WriteLine(tree.ToStringTree()); ParseTreeWalker parseTreeWalker = new ParseTreeWalker(); parseTreeWalker.Walk(myCListener, tree); for (int i = 0; i < myCListener.typeList.Count(); i++) { Console.WriteLine(myCListener.typeList[i]); } }
public string Compile(ICharStream cSource) { CLexer lexer = new CLexer(cSource); CommonTokenStream tokens = new CommonTokenStream(lexer); CParser parser = new CParser(tokens); parser.RemoveErrorListeners(); parser.AddErrorListener(this); try { CParser.CompilationUnitContext compilationUnit = parser.compilationUnit(); ParseTreeWalker walker = new ParseTreeWalker(); walker.Walk(this, compilationUnit); string idents = ""; foreach (CIdentifier ident in CIdentifier.FileScopeIdentifiers) { if (ident.Name.Equals("main")) { idents = ident.Emit() + idents; } else { idents += ident.Emit(); } } return(idents); } catch (SemanticException e) { m_outStream.WriteLine("Compilation Failed: " + e.Message); return(null); } }
public ProcedureSignature BuildSignature(string str, Frame frame) { try { var lexer = new CLexer(new StringReader(str + ";")); var cstate = new ParserState(); var cParser = new CParser(cstate, lexer); var decl = cParser.Parse_ExternalDecl(); var sSig = symbolTable.AddDeclaration(decl) .OfType <SerializedSignature>() .FirstOrDefault(); if (sSig == null) { return(null); } var ser = program.Platform.CreateProcedureSerializer(); return(ser.Deserialize(sSig, frame)); } catch (Exception ex) { Debug.Print("{0}\r\n{1}", ex.Message, ex.StackTrace); return(null); } }
public override TypeLibrary Load(IPlatform platform, TypeLibrary dstLib) { var rdr = new StreamReader(stream); var lexer = new CLexer(rdr, CLexer.StdKeywords); var state = new ParserState(); var parser = new CParser(state, lexer); var symbolTable = CreateSymbolTable(platform, dstLib); var declarations = parser.Parse(); foreach (var decl in declarations) { symbolTable.AddDeclaration(decl); } var slib = new SerializedLibrary { Types = symbolTable.Types.ToArray(), Procedures = symbolTable.Procedures.ToList(), }; var tldser = new TypeLibraryDeserializer(platform, true, dstLib); var tlib = tldser.Load(slib); return(tlib); }
public static void Main(string[] args) { if (args.Length > 0) { string inputFileName = args[0]; if (!Path.IsPathRooted(inputFileName)) { inputFileName = Path.Combine(Environment.CurrentDirectory, inputFileName); } ICharStream input = new ANTLRFileStream(inputFileName); CLexer lex = new CLexer(input); CommonTokenStream tokens = new CommonTokenStream(lex); CParser parser = new CParser(tokens); try { parser.translation_unit(); } catch(RecognitionException re) { Console.Out.WriteLine(re.StackTrace); } } else Console.Error.WriteLine("Usage: cparse <input-file>"); }
do_parse ( string Declaration ) { // // Declare the various streams of characters and tokens for parsing the input // AntlrInputStream input_stream = new AntlrInputStream(Declaration); // Create a stream that reads from the input source CLexer lexer = new CLexer(input_stream); // Create a lexer that feeds off of the input stream CommonTokenStream tokens = new CommonTokenStream(lexer); // Create a buffer of tokens pulled from the lexer CParser parser = new CParser(tokens); // Create a parser that feeds off of the token buffer ParserRuleContext tree = parser.compilationUnit(); // Call the compilationUnit rule in the grammar to build a parse tree from the input C_Listener_Overrides my_listeners = new C_Listener_Overrides(); // Instantiate my listener override functions so they can be used by ParseTreeWalker // // Call all the listeners // ParseTreeWalker.Default.Walk(my_listeners, tree); return(my_listeners.function_def); } // End parse_declaration
private void Lex(string str) { this.lex = new CLexer(new StringReader(str)); }
private void Lex(string str) { lexer = new CLexer(new StringReader(str)); parser = new CParser(parserState, lexer); }
private void CreateLexer(string text) { parserState = new ParserState(); lexer = new CLexer(new StringReader(text)); }