Mercurial > lbo > hg > pcombinators
changeset 36:859c9eaa90c2 draft
Reorganize all imports and separate out ParseState class.
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Wed, 22 May 2019 22:01:10 +0200 |
parents | 6e7d8f8dfba2 |
children | 423f7851fe6d |
files | pcombinators/__init__.py pcombinators/arith_test.py pcombinators/combinators.py pcombinators/primitives.py pcombinators/state.py |
diffstat | 5 files changed, 67 insertions(+), 57 deletions(-) [+] |
line wrap: on
line diff
--- a/pcombinators/__init__.py Wed May 22 22:00:48 2019 +0200 +++ b/pcombinators/__init__.py Wed May 22 22:01:10 2019 +0200 @@ -7,4 +7,5 @@ """ from pcombinators.combinators import * -from pcombinators.primitives import * \ No newline at end of file +from pcombinators.primitives import * +from pcombinators.state import ps \ No newline at end of file
--- a/pcombinators/arith_test.py Wed May 22 22:00:48 2019 +0200 +++ b/pcombinators/arith_test.py Wed May 22 22:01:10 2019 +0200 @@ -6,8 +6,8 @@ @author: lbo """ -from combinators import * -from primitives import * +from pcombinators.combinators import * +from pcombinators.primitives import * def Parens(): """Parentheses contain a term."""
--- a/pcombinators/combinators.py Wed May 22 22:00:48 2019 +0200 +++ b/pcombinators/combinators.py Wed May 22 22:01:10 2019 +0200 @@ -7,59 +7,7 @@ by all Parser's parse() method. """ -def ps(s): - return ParseState(s) - -class ParseState: - """Encapsulates state as the parser goes through input.""" - - _input = '' - _index = 0 - - def __init__(self, s): - """Create a ParseState object from str s, representing the input to be parsed.""" - self._input = s - - def __repr__(self): - if self._index < len(self._input): - return 'ParseState({}< {} >{})'.format( - self._input[0:self._index], self._input[self._index], self._input[self._index+1:]) - else: - return 'ParseState({}<>)'.format(self._input) - - def next(self): - current = self.peek() - self._index += 1 - return current - - def peek(self): - return self._input[self._index] - - def index(self): - return self._index - - def reset(self, ix): - self._index = ix - - def __iter__(self): - return self - - def __next__(self): - return self.next() - - def finished(self): - return self._index == len(self._input) - - def remaining(self): - if self.finished(): - return '' - return self._input[self._index:] - - class ParseException(Exception): - pass - - def error(self, msg): - raise ParseException(msg) +from pcombinators.state import ParseState class Parser: """Super class for all parsers. Implements operator overloading for easier
--- a/pcombinators/primitives.py Wed May 22 22:00:48 2019 +0200 +++ b/pcombinators/primitives.py Wed May 22 22:01:10 2019 +0200 @@ -8,7 +8,7 @@ import re -from combinators import ( +from pcombinators.combinators import ( Parser, ConcatenateResults, OptimisticSequence,
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pcombinators/state.py Wed May 22 22:01:10 2019 +0200 @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Wed May 22 21:41:32 2019 + +@author: lbo +""" + +def ps(s): + return ParseState(s) + +class ParseState: + """Encapsulates state as the parser goes through input.""" + + _input = '' + _index = 0 + + def __init__(self, s): + """Create a ParseState object from str s, representing the input to be parsed.""" + self._input = s + + def __repr__(self): + if self._index < len(self._input): + return 'ParseState({}< {} >{})'.format( + self._input[0:self._index], self._input[self._index], self._input[self._index+1:]) + else: + return 'ParseState({}<>)'.format(self._input) + + def next(self): + current = self.peek() + self._index += 1 + return current + + def peek(self): + return self._input[self._index] + + def index(self): + return self._index + + def reset(self, ix): + self._index = ix + + def __iter__(self): + return self + + def __next__(self): + return self.next() + + def finished(self): + return self._index == len(self._input) + + def remaining(self): + if self.finished(): + return '' + return self._input[self._index:] + + class ParseException(Exception): + pass + + def error(self, msg): + raise ParseException(msg)