コード例 #1
0
ファイル: parser.py プロジェクト: jeamland/gherkin
    def __init__(self, formatter, raise_on_error=True, machine_name='root'):
        self.formatter = formatter
        self.raise_on_error = raise_on_error
        self.machine_name = machine_name

        self.line_offset = None
        self.i18n_language = None
        self.feature_uri = None

        self.listener = FormatterListener(self.formatter)
        self.machines = []
        self.push_machine(machine_name)
        self.lexer = I18nLexer(self)
コード例 #2
0
ファイル: parser.py プロジェクト: jeamland/gherkin
class Parser(object):
    def __init__(self, formatter, raise_on_error=True, machine_name='root'):
        self.formatter = formatter
        self.raise_on_error = raise_on_error
        self.machine_name = machine_name

        self.line_offset = None
        self.i18n_language = None
        self.feature_uri = None

        self.listener = FormatterListener(self.formatter)
        self.machines = []
        self.push_machine(machine_name)
        self.lexer = I18nLexer(self)

    def parse(self, gherkin, feature_uri, line_offset):
        self.feature_uri = feature_uri
        self.formatter.uri(feature_uri)
        self.line_offset = line_offset
        self.lexer.scan(gherkin)

    def __getattr__(self, name):
        attr = getattr(self.listener, name)
        if type(attr) is not types.MethodType:
            raise AttributeError
        def wrapper(*args):
            if args[0] is self:
                args = args[1:]
            if name == u'eof':
                self.event(name, None)
            else:
                self.event(name, args[-1])
            attr(*args)
            if name == u'eof':
                self.pop_machine()
                self.push_machine(self.machine_name)

        setattr(self, name, wrapper)
        return wrapper.__get__(self, self.__class__)

    def event(self, event, line):
        if line:
            line = self.line_offset + line
        else:
            line = None

        self.machine.event(event, line)
        if not self.machine.error:
            return

        state = self.machine.state
        legal_events = self.machine.expected

        if self.raise_on_error:
            raise ParseError(state, event, legal_events, self.feature_uri, line)
        else:
            self.listener.syntax_error(state, event, legal_events,
                                       self.feature_uri, line)

    def push_machine(self, name):
        self.machines.append(Machine(self, name))

    def pop_machine(self):
        self.machines.pop()

    @property
    def machine(self):
        return self.machines[-1]

    @property
    def expected(self):
        return self.machine.expected

    def force_state(self, state):
        self.machine.state = state