Skip to content

Commit

Permalink
added experimental parser using ANTLR4 and the grammar vba.g4 (not in…
Browse files Browse the repository at this point in the history
…tegrated with ViperMonkey yet)
  • Loading branch information
decalage2 committed Feb 3, 2017
1 parent eb60982 commit 6f8a6c5
Show file tree
Hide file tree
Showing 7 changed files with 1,061 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ prettytable
colorlog
colorama
pyparsing
antlr4-python2-runtime
Empty file.
6 changes: 6 additions & 0 deletions vipermonkey/core/antlr_vba/antlr4.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@echo off
set classpath0=%CLASSPATH%
SET CLASSPATH=.;C:\Javalib\antlr-4.6-complete.jar;%CLASSPATH%
java org.antlr.v4.Tool %*
set classpath=%classpath0%
set classpath0=
6 changes: 6 additions & 0 deletions vipermonkey/core/antlr_vba/grun.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@echo off
set classpath0=%CLASSPATH%
SET CLASSPATH=.;C:\Javalib\antlr-4.6-complete.jar;%CLASSPATH%
java org.antlr.v4.gui.TestRig %*
set classpath=%classpath0%
set classpath0=
1 change: 1 addition & 0 deletions vipermonkey/core/antlr_vba/makevba.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
antlr4 -Dlanguage=Python2 -encoding utf8 vba.g4
59 changes: 59 additions & 0 deletions vipermonkey/core/antlr_vba/testvba.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import antlr4
import sys

from vbaLexer import vbaLexer
from vbaListener import vbaListener
from vbaParser import vbaParser

class MyListener(vbaListener):
def __init__(self):
pass

def enterSubStmt(self, ctx):
for child in ctx.children:
# Skip all children that aren't AmbiguousIdentifier
if isinstance(child, vbaParser.AmbiguousIdentifierContext):
# if type(child).__name__ != 'AmbiguousIdentifierContext':
name = child.getText()
print('Sub %r' % name)
# self.that.globals[name.lower()] = ctx

def exitSubStmt(self, ctx):
print('exitSubStmt')

def enterFunctionStmt(self, ctx):
for child in ctx.children:
# Skip all children that aren't AmbiguousIdentifier
if type(child).__name__ != 'AmbiguousIdentifierContext':
continue
name = child.getText()
print('Function %r' % name)
# self.that.globals[name.lower()] = ctx

def enterBlockStmt(self, ctx):
print('enterBlockStmt:')
print(ctx.getText())

def enterLiteral(self, ctx):
print('enterLiteral:')
print(ctx.getText())


try:
filename = sys.argv[1]
except:
sys.exit('Usage: %s <VBA text file>' % sys.argv[0])

print('Parsing %s' % filename)
print('Lexer')
lexer = vbaLexer(antlr4.FileStream(sys.argv[1]))
print('Stream')
stream = antlr4.CommonTokenStream(lexer)
print('vbaParser')
parser = vbaParser(stream)
print('Parsing from startRule')
tree = parser.startRule()
print('Walking the parse tree')
listener = MyListener()
walker = antlr4.ParseTreeWalker()
walker.walk(listener, tree)
Loading

0 comments on commit 6f8a6c5

Please sign in to comment.