Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 55 additions & 14 deletions src/bison/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from __future__ import absolute_import
from __future__ import print_function
import logging.config

import shutil
from os.path import dirname, join
Expand All @@ -37,6 +38,11 @@
from .node import BisonNode
from .convert import bisonToPython

import logging

LOGGER = logging.getLogger(__name__)


WIN_CHOCO_DIR = "C:\\ProgramData\\chocolatey\\lib\\winflexbison3\\tools\\"
WIN_FLEX = join( WIN_CHOCO_DIR, 'win_flex.exe')
WIN_BISON = join( WIN_CHOCO_DIR, 'win_bison.exe')
Expand Down Expand Up @@ -205,6 +211,7 @@ def __init__(self, buildDirectory=None, **kw):
self.defaultNodeClass = nodeClass

self.verbose = kw.get('verbose', False)
self.config_logger()
if self.verbose:
self.bisonCmd.append('--verbose')

Expand Down Expand Up @@ -245,7 +252,7 @@ def _handle(self, targetname, option, names, values):
except:
hdlrline = handler.__init__.__code__.co_firstlineno

print("BisonParser._handle: call handler at line {} with: {}".format(
LOGGER.info("BisonParser._handle: call handler at line {} with: {}".format(
hdlrline, str((targetname, option, names, values)))
)
try:
Expand All @@ -258,7 +265,7 @@ def _handle(self, targetname, option, names, values):
# print("handler for {} returned {}".format(targetname, repr(self.last)))
else:
if self.verbose:
print("no handler for {}, using default".format(targetname))
LOGGER.info("no handler for {}, using default".format(targetname))

cls = self.default_node_class
self.last = cls(target=targetname, option=option, names=names,
Expand All @@ -267,6 +274,41 @@ def _handle(self, targetname, option, names, values):
# assumedly the last thing parsed is at the top of the tree
return self.last

def config_logger(self):
if self.verbose:
level = 'DEBUG'
else:
level = 'INFO'
logging.config.dictConfig({
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
},
},
'handlers': {
'default': {
'level': 'DEBUG',
'formatter': 'standard',
'class': 'logging.StreamHandler',
'stream': 'ext://sys.stdout', # Default is stderr
},
},
'loggers': {
'bison': { # root logger
'handlers': ['default'],
'level': level,
'propagate': True
},
'__main__': { # if __name__ == '__main__'
'handlers': ['default'],
'level': level,
'propagate': False
},
}
})

def handle_timeout(self, signum, frame):
raise TimeoutError("Computation exceeded timeout limit.")

Expand All @@ -293,7 +335,7 @@ def run(self, **kw):
- debug - enables garrulous parser debugging output, default 0
"""
if self.verbose:
print('Parser.run: calling engine')
LOGGER.info('Parser.run: calling engine')

filename = None
# grab keywords
Expand Down Expand Up @@ -329,9 +371,9 @@ def run(self, **kw):


if self.verbose and self.marker:
print('Parser.run(): self.marker (', self.marker, ') is set')
LOGGER.info('Parser.run(): self.marker ({}) is set'.format(self.marker))
if self.verbose and self.file and self.file.closed:
print('Parser.run(): self.file', self.file, 'is closed')
LOGGER.info('Parser.run(): self.file {} is closed'.format(self.file))

error_count = 0
self.last = None
Expand All @@ -352,24 +394,24 @@ def run(self, **kw):
self.report_last_error(filename, e)

if self.verbose:
print('Parser.run: back from engine')
LOGGER.info('Parser.run: back from engine')

if hasattr(self, 'hook_run'):
self.last = self.hook_run(filename, self.last)

if self.verbose and not self.marker:
print('last:', self.last)
LOGGER.info('last:{}'.format(self.last))

if self.verbose:
print('last:', self.last)
LOGGER.info('last:{}'.format(self.last))

# restore old values
self.file = oldfile
self.read = oldread
self.marker = 0

if self.verbose:
print('------------------ result=', self.last)
LOGGER.info('------------------ result={}'.format(self.last))

# close the file if we opened one
if i_opened_a_file and fileobj:
Expand All @@ -390,14 +432,13 @@ def read(self, nbytes):
"""
# default to stdin
if self.verbose:
print('Parser.read: want %s bytes' % nbytes)
LOGGER.info('Parser.read: want %s bytes' % nbytes)

_bytes = self.file.readline(nbytes).replace(b'\r\n', b'\n').replace(b'\r', b'\n')

if self.verbose:
print('Parser.read: got %s bytes' % len(_bytes))
print(_bytes)

LOGGER.info('Parser.read: got %s bytes' % len(_bytes))
LOGGER.info(_bytes)
return _bytes

def report_last_error(self, filename, error):
Expand Down Expand Up @@ -437,7 +478,7 @@ def report_last_error(self, filename, error):
if self.verbose:
traceback.print_exc()

print('ERROR:', error)
LOGGER.error(error)

def report_syntax_error(self, msg, yytext, first_line, first_col, last_line, last_col):

Expand Down
Loading