#
# DO NOT EDIT THIS FILE!
#
# Parser generated by BisonGen on Sun Jun 10 00:13:21 2001.
#


# token definitions
NCName = 257
QName = 258
Integer = 259
StringWithoutParens = 260

# vector mapping lexer token numbers into internal token numbers
token_translations = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 8, 
9, 2, 2, 2, 2, 2, 7, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 
3, 4, 5, 6]

YYTRANSLATE = lambda x: x > 260 and 19 or token_translations[x]

# vector of items of all rules.
rhs_tokens = [None,
              [13],
              [11],
              [14],
              [12],
              [13, 12],
              [7, 5],
              [12, 7, 5],
              [3],
              [4],
              [15],
              [14, 15],
              [16, 8, 17, 9],
              [3],
              [18],
              [17, 18],
              [],
              [6],
              [8, 18, 9],
              [8, 18, 9, 6],
              [6, 8, 18, 9],
              [6, 8, 18, 9, 6],
              ]

# vector of line numbers and filename of all rules
rule_info = [": line 0",
             "XPointer/XPointer.bgen: line 24",
             "XPointer/XPointer.bgen: line 33",
             "XPointer/XPointer.bgen: line 42",
             "XPointer/XPointer.bgen: line 57",
             "XPointer/XPointer.bgen: line 60",
             "XPointer/XPointer.bgen: line 79",
             "XPointer/XPointer.bgen: line 91",
             "XPointer/XPointer.bgen: line 111",
             "XPointer/XPointer.bgen: line 114",
             "XPointer/XPointer.bgen: line 123",
             "XPointer/XPointer.bgen: line 134",
             "XPointer/XPointer.bgen: line 155",
             "XPointer/XPointer.bgen: line 174",
             "XPointer/XPointer.bgen: line 182",
             "XPointer/XPointer.bgen: line 185",
             "XPointer/XPointer.bgen: line 220",
             "XPointer/XPointer.bgen: line 228",
             "XPointer/XPointer.bgen: line 239",
             "XPointer/XPointer.bgen: line 262",
             "XPointer/XPointer.bgen: line 288",
             "XPointer/XPointer.bgen: line 314",
             ]

# vector of string-names indexed by token number
token_names = ['$',
               'error',
               '$undefined.',
               'NCName',
               'QName',
               'Integer',
               'StringWithoutParens',
               '/',
               '(',
               ')',
               'XPointer',
               'ChildSeq',
               'Sequence',
               'Name',
               'FullXPtr',
               'XPtrPart',
               'Scheme',
               'SchemeSpecificExpr',
               'StringWithBalancedParens',
               '0',
               ]

# symbol number of symbol that rule derives.
derives = [0, 10, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 16, 17, 17, 
18, 18, 18, 18, 18, 18]

# number of symbols composing right hand side of rule.
rhs_size = [0, 1, 1, 1, 1, 2, 2, 3, 1, 1, 1, 2, 4, 1, 1, 2, 0, 1, 3, 4, 
4, 5]

# default rule to reduce with in state. 0 means the default is an error.
# indexed by state number
default_action = [0, 8, 9, 0, 2, 4, 1, 3, 10, 0, 6, 0, 5, 13, 11, 16, 7, 
17, 16, 0, 14, 16, 0, 12, 15, 0, 18, 20, 19, 21, 0, 0, 0]

# default state to go to after a reduction of a rule.
# indexed by variable number (lhs token)
default_goto = [30, 4, 5, 6, 7, 8, 9, 19, 20]

# index in yytable of the portion describing state (indexed by state number)
# If the value in yytable is positive, we shift the token and go to that state.
# If the value is negative, it is minus a rule number to reduce by.
# If the value is zero, the default action from yydefact[s] is used.
action_idx = [-3, -6, -32768, -2, -32768, 7, 8, 3, -32768, 9, -32768, 11, 
7, -32768, -32768, 5, -32768, 10, 5, -1, -32768, 5, 12, -32768, -32768, 
13, 14, 17, -32768, -32768, 19, 24, -32768]

# the index in yytable of the portion describing what to do after reducing a rule.
# The value from yytable is the state to go to.
goto_idx = [-32768, -32768, 20, -32768, -32768, 18, -32768, -32768, -9]

# a vector filled with portions for different uses. (using action_idx and goto_idx)
yytable = [1, 2, -13, 10, 3, 17, 13, 18, 23, 22, 24, 17, 25, 18, 11, 3, 
16, 15, 21, 31, 28, 26, 27, 29, 32, 14, 12]

# a vector indexed in parallel with yytable.
# It indicates the bounds of the portion you are trying to examine.
yycheck = [3, 4, 8, 5, 7, 6, 3, 8, 9, 18, 19, 6, 21, 8, 7, 7, 5, 8, 8, 
0, 6, 9, 9, 6, 0, 7, 6]

YYLAST = 26
YYFINAL = 32
YYFLAG = -32768
YYNTBASE = 10

# Static definitions
YYEMPTY = -2
YYEOF = 0
YYINITDEPTH = 1000

LEXER_FUNCTIONS = 0

class Parser:
    def __init__(self, verbose=0):
        self.verbose = verbose
        
    def debug_mode(self, flag=None):
        if flag is None:
            return self.verbose

        if type(flag) != type(1):
            raise TypeError('an integer is required')
        self.verbose = flag
        return flag

    def parse(self, text):
        state_stack = [0]*YYINITDEPTH
        value_stack = [0]*YYINITDEPTH

        
        lexer_pos = 0
        lexer_end = len(text)
        lexer_state = INITIAL
        lexer_last = 0
        yylval = ''

        yyline = 1
        yycolumn = 1
        
        yystate = 0
        yychar = YYEMPTY        # cause a token to be read
        
        # Initialize stack pointers
        # Waste one element of value and location stack
        # so that they stay on the same level as the state stack.
        # The wasted elements are never initialized.
        state_ptr = -1
        value_ptr = 0
        
        while 1:
            # Push a new state, which is found in yystate.
            # In all cases, when you get here, the value and location stacks
            # have just been pushed.  So pushing a state here evens the stacks.
            state_ptr = state_ptr + 1
            state_stack[state_ptr] = yystate
        
            
            # Do appropriate processing given the current state.
            # Read a lookahead token if we need one and don't already have one.
        
            # First try to decide what to do without reference to lookahead token.
            yyn = action_idx[yystate]
            if yyn == YYFLAG:
                yyn = default_action[yystate]
                if yyn == 0:
                    self.report_error(yystate, yyline, yycolumn, yylval)
                    return 1
                # Do a reduction. yyn is the number of a rule to reduce with.
        
                state_ptr = state_ptr - rhs_size[yyn]
                value_ptr = value_ptr - rhs_size[yyn]
        
                if action_routines[yyn]:
                    yyval = action_routines[yyn](self, value_stack, value_ptr)
                    value_ptr = value_ptr + 1
                    value_stack[value_ptr] = yyval
                else:
                    value_ptr = value_ptr + 1

        
                # Now "shift" the result of the reduction.
                # Determine what state that goes to, based on the state
                # we popped back to and the rule number reduced by.
        
                yyn = derives[yyn] - YYNTBASE
                yystate = goto_idx[yyn] + state_stack[state_ptr]
                if 0 <= yystate <= YYLAST and yycheck[yystate] == state_stack[state_ptr]:
                    yystate = yytable[yystate]
                else:
                    yystate = default_goto[yyn]
                continue
            
            # Not known => get a lookahead token if don't already have one.
        
            # yychar is either YYEMPTY, YYEOF or a valid token in external form
            if yychar == YYEMPTY:
                # Setup line and column numbers
                for ch in yylval:
                    if ch == '\n':
                        yyline = yyline + 1
                        yycolumn = 1
                    else:
                        yycolumn = yycolumn + 1

                ### Lexical analysis ###
                while lexer_last < lexer_end:
                    lexer_pos = lexer_last
                    try:
                        match = patterns[lexer_state].match(text, lexer_pos)
                        matched = reduce(lambda result, item:
                                         item[1] is None and result or item,
                                         match.groupdict().items(),
                                         (None, None))
                    except AttributeError:
                        # comes here when match is none, it is an error anyway
                        self.error('No action found for "%s"' % text[lexer_pos:])

                    lexer_last = lexer_last + len(matched[1])
                    lexer_action = pattern_actions[matched[0]]
                    
                    if lexer_action:
                        lexer_state = lexer_action[0] or lexer_state
                        if len(lexer_action) > 1:
                            yylval = matched[1]
                            yychar = lexer_action[1] or ord(yylval)
                            break
                        else:
                            # Just a state change, reprocess the text
                            lexer_last = lexer_pos
                    else:
                        # throw away matched text and update position
                        for ch in matched[1]:
                            if ch == '\n':
                                yyline = yyline + 1
                                yycolumn = 1
                            else:
                                yycolumn = yycolumn + 1
                    continue
                else:
                    # Reached end of input
                    yychar = YYEOF
            # Convert token to internal form (in yychar1) for indexing tables with

            if yychar <= 0:
                # This means end-of-input.
                yychar1 = 0
            else:
                yychar1 = YYTRANSLATE(yychar)
                yyn = yyn + yychar1

            if yyn < 0 or yyn > YYLAST or  yycheck[yyn] != yychar1:
                # comes here after end of input
                yyn = default_action[yystate]
                if yyn == 0:
                    self.report_error(yystate, yyline, yycolumn, yylval)
                    return None
                # Do a reduction. yyn is the number of a rule to reduce with.
        
                state_ptr = state_ptr - rhs_size[yyn]
                value_ptr = value_ptr - rhs_size[yyn]
        
                if action_routines[yyn]:
                    yyval = action_routines[yyn](self, value_stack, value_ptr)
                    value_ptr = value_ptr + 1
                    value_stack[value_ptr] = yyval
                else:
                    value_ptr = value_ptr + 1

        
                # Now "shift" the result of the reduction.
                # Determine what state that goes to, based on the state
                # we popped back to and the rule number reduced by.
        
                yyn = derives[yyn] - YYNTBASE
                yystate = goto_idx[yyn] + state_stack[state_ptr]
                if 0 <= yystate <= YYLAST and yycheck[yystate] == state_stack[state_ptr]:
                    yystate = yytable[yystate]
                else:
                    yystate = default_goto[yyn]
                continue
            
            yyn = yytable[yyn]
        
            # yyn is what to do for this token type in this state.
            # Negative => reduce, -yyn is rule number.
            # Positive => shift, yyn is new state.
            #   New state is final state => don't bother to shift
            #   just return success.
            # 0, or max negative number => error.
        
            if YYFLAG < yyn < 0:
                yyn = -yyn
                # Do a reduction. yyn is the number of a rule to reduce with.
        
                state_ptr = state_ptr - rhs_size[yyn]
                value_ptr = value_ptr - rhs_size[yyn]
        
                if action_routines[yyn]:
                    yyval = action_routines[yyn](self, value_stack, value_ptr)
                    value_ptr = value_ptr + 1
                    value_stack[value_ptr] = yyval
                else:
                    value_ptr = value_ptr + 1

        
                # Now "shift" the result of the reduction.
                # Determine what state that goes to, based on the state
                # we popped back to and the rule number reduced by.
        
                yyn = derives[yyn] - YYNTBASE
                yystate = goto_idx[yyn] + state_stack[state_ptr]
                if 0 <= yystate <= YYLAST and yycheck[yystate] == state_stack[state_ptr]:
                    yystate = yytable[yystate]
                else:
                    yystate = default_goto[yyn]
                continue
            elif yyn == YYFINAL:
                return value_stack[value_ptr-1]
            elif yyn <= 0:
                # Now it is either 0 or YYFLAG
                self.report_error(yystate, yyline, yycolumn, yylval)
                return None
            
            # Shift the lookahead token.
        
 
            if yychar != YYEOF:
                yychar = YYEMPTY
            
            value_ptr = value_ptr + 1
            value_stack[value_ptr] = yylval
        
            yystate = yyn
            continue
        # should never get here
        return None
        
    def report_error(self, state, line, column, lval):
        ruleno = action_idx[state]
        msg = "parse error at line %d, column %d: matched '%s'" % (
            line, column, lval)
        if YYFLAG < ruleno < YYLAST:
            # Start X at -ruleno to avoid negative indexes in yycheck
            start = ruleno < 0 and -ruleno or 0
            first = 1
            for x in range(start, len(token_names)):
                if (x + ruleno) < len(yycheck) and yycheck[x + ruleno] == x:
                    if first:
                        msg = msg + ", expecting"
                        first = 0
                    else:
                        msg = msg + " or"
                    msg = msg + " '%s'" % token_names[x]
        self.error(msg)
        return
        
    def announce(self, format, *args):
        sys.stderr.write(format % args)
        return

    def error(self, format, *args):
        raise SyntaxError(format % args)

    def print_reduce(self, rule):
        sys.stderr.write("Reducing via rule %d (%s), " % (rule, rule_info[rule]))
        # print the symbols being reduced and their result.
        for token in rhs_tokens[rule]:
            sys.stderr.write("%s " % token_names[token])
        sys.stderr.write("-> %s\n" % token_names[derives[rule]])
        return

    def print_state_stack(self, stack, size):
        sys.stderr.write("state stack now")
        for i in range(size+1):
            sys.stderr.write(" %d" % stack[i])
        sys.stderr.write("\n")
        return

new = Parser

# modules required for action routines
from Ft.XPointer import XPointer
from Ft.XPointer import XPtrPart

# the action code for each rule
def XPointer1(self, __stack, __ptr):
    """
    from XPointer/XPointer.bgen, line 24
    XPointer: Name
    """
    __val = XPointer.BareName(__stack[__ptr+1])
    return __val

def XPointer2(self, __stack, __ptr):
    """
    from XPointer/XPointer.bgen, line 33
    XPointer: ChildSeq
    """
    __val = XPointer.ChildSequence(__stack[__ptr+1])
    return __val

def XPointer3(self, __stack, __ptr):
    """
    from XPointer/XPointer.bgen, line 42
    XPointer: FullXPtr
    """
    __val = XPointer.FullXPtr(__stack[__ptr+1])
    return __val

def ChildSeq2(self, __stack, __ptr):
    """
    from XPointer/XPointer.bgen, line 60
    ChildSeq: Name Sequence
    """
    __stack[__ptr+2].insert(0, XPointer.BareName(__stack[__ptr+1]))
    __val = __stack[__ptr+2]
    return __val

def Sequence1(self, __stack, __ptr):
    """
    from XPointer/XPointer.bgen, line 79
    Sequence: '/' Integer
    """
    __val = [int(__stack[__ptr+2])]
    return __val

def Sequence2(self, __stack, __ptr):
    """
    from XPointer/XPointer.bgen, line 91
    Sequence: Sequence '/' Integer
    """
    __stack[__ptr+1].append(int(__stack[__ptr+3]))
    __val = __stack[__ptr+1]
    return __val

def FullXPtr1(self, __stack, __ptr):
    """
    from XPointer/XPointer.bgen, line 123
    FullXPtr: XPtrPart
    """
    __val = [__stack[__ptr+1]]
    return __val

def FullXPtr2(self, __stack, __ptr):
    """
    from XPointer/XPointer.bgen, line 134
    FullXPtr: FullXPtr XPtrPart
    """
    __stack[__ptr+1].append(__stack[__ptr+2])
    __val = __stack[__ptr+1]
    return __val

def XPtrPart1(self, __stack, __ptr):
    """
    from XPointer/XPointer.bgen, line 155
    XPtrPart: Scheme '(' SchemeSpecificExpr ')'
    """
    lexer.state = INITIAL;
    __val = XPtrPart.Scheme(__stack[__ptr+1], __stack[__ptr+3])
    return __val

def SchemeSpecificExpr2(self, __stack, __ptr):
    """
    from XPointer/XPointer.bgen, line 185
    SchemeSpecificExpr: SchemeSpecificExpr StringWithBalancedParens
    """
    __stack[__ptr+1].extend(__stack[__ptr+2])
    __val = __stack[__ptr+1]
    return __val

def StringWithBalancedParens1(self, __stack, __ptr):
    """
    from XPointer/XPointer.bgen, line 220
    StringWithBalancedParens:
    """
    __val = []
    return __val

def StringWithBalancedParens2(self, __stack, __ptr):
    """
    from XPointer/XPointer.bgen, line 228
    StringWithBalancedParens: StringWithoutParens
    """
    __val = [__stack[__ptr+1]]
    return __val

def StringWithBalancedParens3(self, __stack, __ptr):
    """
    from XPointer/XPointer.bgen, line 239
    StringWithBalancedParens: '(' StringWithBalancedParens ')'
    """
    __val = [__stack[__ptr+1]] + __stack[__ptr+2] + [__stack[__ptr+3]]
    return __val

def StringWithBalancedParens4(self, __stack, __ptr):
    """
    from XPointer/XPointer.bgen, line 262
    StringWithBalancedParens: '(' StringWithBalancedParens ')' StringWithoutParens
    """
    __val = [__stack[__ptr+1]] + __stack[__ptr+2] + [__stack[__ptr+3], __stack[__ptr+4]]
    return __val

def StringWithBalancedParens5(self, __stack, __ptr):
    """
    from XPointer/XPointer.bgen, line 288
    StringWithBalancedParens: StringWithoutParens '(' StringWithBalancedParens ')'
    """
    __val = [__stack[__ptr+1], __stack[__ptr+2]] + __stack[__ptr+3] + [__stack[__ptr+4]]
    return __val

def StringWithBalancedParens6(self, __stack, __ptr):
    """
    from XPointer/XPointer.bgen, line 314
    StringWithBalancedParens: StringWithoutParens '(' StringWithBalancedParens ')' StringWithoutParens
    """
    __val = [__stack[__ptr+1], __stack[__ptr+2]] + __stack[__ptr+3] + [__stack[__ptr+4], __stack[__ptr+5]]
    return __val

action_routines = [None,
                   XPointer1,
                   XPointer2,
                   XPointer3,
                   None,
                   ChildSeq2,
                   Sequence1,
                   Sequence2,
                   None,
                   None,
                   FullXPtr1,
                   FullXPtr2,
                   XPtrPart1,
                   None,
                   None,
                   SchemeSpecificExpr2,
                   StringWithBalancedParens1,
                   StringWithBalancedParens2,
                   StringWithBalancedParens3,
                   StringWithBalancedParens4,
                   StringWithBalancedParens5,
                   StringWithBalancedParens6,
                   ]

# start condition definitions for the lexer
INITIAL = 1
SCHEME_EXPR = 2

# the expressions and information for each rule
import re
patterns = {
    INITIAL : re.compile('(?P<p00>[a-zA-Z_][a-zA-Z0-9\\.\\-_]*)|(?P<p01>[a-zA-Z_][a-zA-Z0-9\\.\\-_]*:[a-zA-Z_][a-zA-Z0-9\\.\\-_]*)|(?P<p02>[1-9][0-9]*)|(?P<p03>\\()|(?P<p04>\\s+)|(?P<p05>.)', re.M),
    SCHEME_EXPR : re.compile('(?P<p06>\\(|\\))|(?P<p07>(\\^[)\\^]|[^()])*)', re.M),
}
pattern_actions = {
    'p00' : (None, NCName),
    'p01' : (None, QName),
    'p02' : (None, Integer),
    'p03' : (SCHEME_EXPR, None),
    'p04' : None,
    'p05' : (None, None),
    'p06' : (None, None),
    'p07' : (None, StringWithoutParens),
}
if __name__ == '__main__':
    import sys
    try: import readline
    except: pass

    try:
        import XPointerParserc
        parser = XPointerParserc.new(1)
        print 'Using C parser'
    except:
        import XPointerParser
        parser = XPointerParser.new(1)
        print 'Using Python parser'

    if len(sys.argv) > 1:
        result = parser.parse(sys.argv[1])
        result.pprint()
        raise SystemExit()
    print 'Use <Ctrl>-C to exit.'
    try:
        while 1:
            expr = raw_input('>>>')
            result = parser.parse(expr)
            result.pprint()
    except KeyboardInterrupt:
        raise SystemExit
