pyPEG.py
author Volker Birk <vb@pep-project.org>
Fri, 25 Jan 2019 23:31:57 +0100
changeset 25 cb4a7f8b230d
parent 0 76005e62091d
permissions -rw-r--r--
...
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     1
# YPL parser 1.5
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     2
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     3
# written by VB.
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     4
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     5
import re
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     6
import sys, codecs
25
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
     7
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
     8
class keyword(str): pass
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
     9
class code(str): pass
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    10
class ignore(object):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    11
    def __init__(self, regex_text, *args):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    12
        self.regex = re.compile(regex_text, *args)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    13
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    14
class _and(object):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    15
    def __init__(self, something):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    16
        self.obj = something
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    17
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    18
class _not(_and): pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    19
25
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
    20
class Name(str):
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    21
    def __init__(self, *args):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    22
        self.line = 0
25
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
    23
        self.file = ""
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    24
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    25
class Symbol(list):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    26
    def __init__(self, name, what):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    27
        self.__name__ = name
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    28
        self.append(name)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    29
        self.what = what
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    30
        self.append(what)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    31
    def __call__(self):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    32
        return self.what
25
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
    33
    def __str__(self):
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
    34
        return 'Symbol(' + repr(self.__name__) + ', ' + repr(self.what) + ')'
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    35
    def __repr__(self):
25
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
    36
        return str(self)
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
    37
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
    38
word_regex = re.compile(r"\w+")
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
    39
rest_regex = re.compile(r".*")
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    40
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    41
print_trace = False
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    42
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    43
def skip(skipper, text, skipWS, skipComments):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    44
    if skipWS:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    45
        t = text.lstrip()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    46
    else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    47
        t = text
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    48
    if skipComments:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    49
        try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    50
            while True:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    51
                skip, t = skipper.parseLine(t, skipComments, [], skipWS, None)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    52
                if skipWS:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    53
                    t = t.lstrip()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    54
        except: pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    55
    return t
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    56
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    57
class parser(object):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    58
    def __init__(self, another = False, p = False):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    59
        self.restlen = -1 
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    60
        if not(another):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    61
            self.skipper = parser(True, p)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    62
            self.skipper.packrat = p
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    63
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    64
            self.skipper = self
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    65
        self.lines = None
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    66
        self.textlen = 0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    67
        self.memory = {}
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    68
        self.packrat = p
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    69
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    70
    # parseLine():
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    71
    #   textline:       text to parse
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    72
    #   pattern:        pyPEG language description
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    73
    #   resultSoFar:    parsing result so far (default: blank list [])
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    74
    #   skipWS:         Flag if whitespace should be skipped (default: True)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    75
    #   skipComments:   Python functions returning pyPEG for matching comments
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    76
    #   
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    77
    #   returns:        pyAST, textrest
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    78
    #
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    79
    #   raises:         SyntaxError(reason) if textline is detected not being in language
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    80
    #                   described by pattern
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    81
    #
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    82
    #                   SyntaxError(reason) if pattern is an illegal language description
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    83
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    84
    def parseLine(self, textline, pattern, resultSoFar = [], skipWS = True, skipComments = None):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    85
        name = None
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    86
        _textline = textline
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    87
        _pattern = pattern
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    88
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    89
        def R(result, text):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    90
            if __debug__:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    91
                if print_trace:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    92
                    try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    93
                        if _pattern.__name__ != "comment":
25
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
    94
                            sys.stderr.write("match: " + _pattern.__name__ + "\n")
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    95
                    except: pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    96
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    97
            if self.restlen == -1:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    98
                self.restlen = len(text)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    99
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   100
                self.restlen = min(self.restlen, len(text))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   101
            res = resultSoFar
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   102
            if name and result:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   103
                name.line = self.lineNo()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   104
                res.append(Symbol(name, result))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   105
            elif name:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   106
                name.line = self.lineNo()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   107
                res.append(Symbol(name, []))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   108
            elif result:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   109
                if type(result) is type([]):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   110
                    res.extend(result)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   111
                else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   112
                    res.extend([result])
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   113
            if self.packrat:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   114
                self.memory[(len(_textline), id(_pattern))] = (res, text)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   115
            return res, text
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   116
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   117
        def syntaxError():
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   118
            if self.packrat:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   119
                self.memory[(len(_textline), id(_pattern))] = False
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   120
            raise SyntaxError()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   121
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   122
        if self.packrat:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   123
            try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   124
                result = self.memory[(len(textline), id(pattern))]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   125
                if result:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   126
                    return result
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   127
                else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   128
                    raise SyntaxError()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   129
            except: pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   130
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   131
        if callable(pattern):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   132
            if __debug__:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   133
                if print_trace:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   134
                    try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   135
                        if pattern.__name__ != "comment":
25
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
   136
                            sys.stderr.write("testing with " + pattern.__name__ + ": " + textline[:40] + "\n")
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   137
                    except: pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   138
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   139
            if pattern.__name__[0] != "_":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   140
                name = Name(pattern.__name__)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   141
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   142
            pattern = pattern()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   143
            if callable(pattern):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   144
                pattern = (pattern,)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   145
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   146
        text = skip(self.skipper, textline, skipWS, skipComments)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   147
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   148
        pattern_type = type(pattern)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   149
25
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
   150
        if pattern_type is str:
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   151
            if text[:len(pattern)] == pattern:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   152
                text = skip(self.skipper, text[len(pattern):], skipWS, skipComments)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   153
                return R(None, text)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   154
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   155
                syntaxError()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   156
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   157
        elif pattern_type is keyword:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   158
            m = word_regex.match(text)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   159
            if m:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   160
                if m.group(0) == pattern:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   161
                    text = skip(self.skipper, text[len(pattern):], skipWS, skipComments)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   162
                    return R(None, text)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   163
                else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   164
                    syntaxError()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   165
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   166
                syntaxError()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   167
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   168
        elif pattern_type is _not:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   169
            try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   170
                r, t = self.parseLine(text, pattern.obj, [], skipWS, skipComments)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   171
            except:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   172
                return resultSoFar, textline
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   173
            syntaxError()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   174
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   175
        elif pattern_type is _and:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   176
            r, t = self.parseLine(text, pattern.obj, [], skipWS, skipComments)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   177
            return resultSoFar, textline
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   178
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   179
        elif pattern_type is type(word_regex) or pattern_type is ignore:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   180
            if pattern_type is ignore:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   181
                pattern = pattern.regex
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   182
            m = pattern.match(text)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   183
            if m:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   184
                text = skip(self.skipper, text[len(m.group(0)):], skipWS, skipComments)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   185
                if pattern_type is ignore:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   186
                    return R(None, text)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   187
                else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   188
                    return R(m.group(0), text)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   189
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   190
                syntaxError()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   191
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   192
        elif pattern_type is tuple:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   193
            result = []
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   194
            n = 1
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   195
            for p in pattern:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   196
                if type(p) is type(0):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   197
                    n = p
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   198
                else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   199
                    if n>0:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   200
                        for i in range(n):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   201
                            result, text = self.parseLine(text, p, result, skipWS, skipComments)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   202
                    elif n==0:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   203
                        if text == "":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   204
                            pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   205
                        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   206
                            try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   207
                                newResult, newText = self.parseLine(text, p, result, skipWS, skipComments)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   208
                                result, text = newResult, newText
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   209
                            except SyntaxError:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   210
                                pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   211
                    elif n<0:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   212
                        found = False
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   213
                        while True:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   214
                            try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   215
                                newResult, newText = self.parseLine(text, p, result, skipWS, skipComments)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   216
                                result, text, found = newResult, newText, True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   217
                            except SyntaxError:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   218
                                break
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   219
                        if n == -2 and not(found):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   220
                            syntaxError()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   221
                    n = 1
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   222
            return R(result, text)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   223
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   224
        elif pattern_type is list:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   225
            result = []
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   226
            found = False
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   227
            for p in pattern:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   228
                try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   229
                    result, text = self.parseLine(text, p, result, skipWS, skipComments)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   230
                    found = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   231
                except SyntaxError:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   232
                    pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   233
                if found:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   234
                    break
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   235
            if found:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   236
                return R(result, text)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   237
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   238
                syntaxError()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   239
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   240
        else:
25
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
   241
            raise SyntaxError("illegal type in grammar: " + pattern_type)
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   242
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   243
    def lineNo(self):
25
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
   244
        if not(self.lines): return ""
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
   245
        if self.restlen == -1: return ""
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   246
        parsed = self.textlen - self.restlen
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   247
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   248
        left, right = 0, len(self.lines)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   249
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   250
        while True:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   251
            mid = int((right + left) / 2)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   252
            if self.lines[mid][0] <= parsed:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   253
                try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   254
                    if self.lines[mid + 1][0] >= parsed:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   255
                        try:
25
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
   256
                            return self.lines[mid + 1][1] + ":" + self.lines[mid + 1][2]
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   257
                        except:
25
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
   258
                            return ""
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   259
                    else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   260
                        left = mid + 1
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   261
                except:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   262
                    try:
25
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
   263
                        return self.lines[mid + 1][1] + ":" + self.lines[mid + 1][2]
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   264
                    except:
25
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
   265
                        return ""
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   266
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   267
                right = mid - 1
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   268
            if left > right:
25
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
   269
                return ""
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   270
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   271
# plain module API
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   272
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   273
def parseLine(textline, pattern, resultSoFar = [], skipWS = True, skipComments = None, packrat = False):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   274
    p = parser(p=packrat)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   275
    text = skip(p.skipper, textline, skipWS, skipComments)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   276
    ast, text = p.parseLine(text, pattern, resultSoFar, skipWS, skipComments)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   277
    return ast, text
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   278
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   279
# parse():
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   280
#   language:       pyPEG language description
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   281
#   lineSource:     a fileinput.FileInput object
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   282
#   skipWS:         Flag if whitespace should be skipped (default: True)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   283
#   skipComments:   Python function which returns pyPEG for matching comments
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   284
#   packrat:        use memoization
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   285
#   lineCount:      add line number information to AST
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   286
#   
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   287
#   returns:        pyAST
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   288
#
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   289
#   raises:         SyntaxError(reason), if a parsed line is not in language
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   290
#                   SyntaxError(reason), if the language description is illegal
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   291
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   292
def parse(language, lineSource, skipWS = True, skipComments = None, packrat = False, lineCount = True):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   293
    lines, lineNo = [], 0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   294
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   295
    while callable(language):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   296
        language = language()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   297
25
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
   298
    orig, ld = "", 0
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   299
    for line in lineSource:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   300
        if lineSource.isfirstline():
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   301
            ld = 1
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   302
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   303
            ld += 1
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   304
        lines.append((len(orig), lineSource.filename(), lineSource.lineno() - 1))
25
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
   305
        orig += line
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   306
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   307
    textlen = len(orig)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   308
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   309
    try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   310
        p = parser(p=packrat)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   311
        p.textlen = len(orig)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   312
        if lineCount:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   313
            p.lines = lines
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   314
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   315
            p.line = None
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   316
        text = skip(p.skipper, orig, skipWS, skipComments)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   317
        result, text = p.parseLine(text, language, [], skipWS, skipComments)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   318
        if text:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   319
            raise SyntaxError()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   320
25
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
   321
    except SyntaxError as msg:
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   322
        parsed = textlen - p.restlen
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   323
        textlen = 0
25
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
   324
        nn, lineNo, file = 0, 0, ""
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   325
        for n, ld, l in lines:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   326
            if n >= parsed:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   327
                break
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   328
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   329
                lineNo = l
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   330
                nn += 1
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   331
                file = ld
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   332
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   333
        lineNo += 1
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   334
        nn -= 1
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   335
        lineCont = orig.splitlines()[nn]
25
Volker Birk <vb@pep-project.org>
parents: 0
diff changeset
   336
        raise SyntaxError("syntax error in " + file + ":" + lineNo + ": " + lineCont)
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   337
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   338
    return result