Add loggin when running the parser as main
@@ -501,7 +501,6 @@ class CalvinParser(object):
}
self.issuetracker.add_error('Syntax error.', info)
- # Trying to recover from here...
def _find_column(self, lexpos):
last_cr = self.source_text.rfind('\n', 0, lexpos)
@@ -518,14 +517,15 @@ class CalvinParser(object):
}
return info
- def parse(self, source_text):
+
+ def parse(self, source_text, logger=None):
# return ir (AST) and issuetracker
self.issuetracker = IssueTracker()
self.source_text = source_text
root = None
try:
- root = self.parser.parse(source_text)
+ root = self.parser.parse(source_text, debug=logger)
except SyntaxError as e:
self.issuetracker.add_error(e.text, {'line':e.lineno, 'col':e.offset})
finally:
@@ -542,12 +542,21 @@ def calvin_parse(source_text):
if __name__ == '__main__':
+ import os
import sys
import json
import astprint
+ import logging
+ logging.basicConfig(
+ level = logging.DEBUG,
+ filename = "{}/parselog.txt".format(os.path.dirname(os.path.realpath(__file__))),
+ filemode = "w",
+ format = "%(filename)10s:%(lineno)4d:%(message)s"
+ )
if len(sys.argv) < 2:
+ log = logging.getLogger()
script = 'inline'
source_text = \
'''
@@ -606,7 +615,8 @@ apply actor, actor: some_rule | node_attr(node_spec=NODE1) ~ current()
print "Error: Could not read file: '%s'" % script
sys.exit(1)
- ir, deploy_ir, it = calvin_parse(source_text)
+ parser = CalvinParser()
+ ir, deploy_ir, it = parser.parse(source_text, logger=log)
if it.issue_count == 0:
print "No issues"
for i in it.formatted_issues(custom_format="{type!c}: {reason} {filename}:{line}:{col}", filename=script):
