Parse code using antlr.
- The lexer and parser classes in CodeParser are generated by Antlr grammar file.
- The CodeParser.Test demonstrates how to parse source code file to extract class and interface with their properties and methods. By now, the Java, C#, Python, PHP, JavaScript, C++, C language and MySql have been included.
- The CodeParser.Viewer is a visual tool to analyse the parser rules.
1. Python
- source code
#!/usr/bin/python3
class HelloWorld():
owner = ""
target = ""
def sayHello(self, word):
print(word)
x = HelloWorld()
x.sayHello("Hello")
- parser test output
Class:HelloWorld
{
Field:owner
Field:target
Method:sayHello
}
2. PHP
- source code
<?
namespace demo;
use ArrayObject;
$count = 0;
class HelloWorld {
public string $owner;
public string $target;
function sayHello(?string $word)
{
echo $word;
}
}
function test()
{
$helloWorld = new HelloWorld();
$helloWorld->sayHello();
}
?>
- parser test output
Namespace:demo
Use:ArrayObject
Field:$count
Class:HelloWorld
{
Field:$owner
Field:$target
Method:sayHello
}
Function:test
The library SqlAnalyser.Core of DatabaseManager project uses the TSqlParser, MySqlParser and PlSqlParser to parse and translate view, function, procedure and table trigger between SqlServer, MySql and Oracle.