<s> import array <EOL> import string <EOL> import types <EOL> from sys import maxint <EOL> import Errors <EOL> BOL = '<STR_LIT>' <EOL> EOL = '<STR_LIT>' <EOL> EOF = '<STR_LIT>' <EOL> nl_code = ord ( '<STR_LIT:\n>' ) <EOL> def chars_to_ranges ( s ) : <EOL> """<STR_LIT>""" <EOL> char_list = list ( s ) <EOL> char_list . sort ( ) <EOL> i = <NUM_LIT:0> <EOL> n = len ( char_list ) <EOL> result = [ ] <EOL> while i < n : <EOL> code1 = ord ( char_list [ i ] ) <EOL> code2 = code1 + <NUM_LIT:1> <EOL> i = i + <NUM_LIT:1> <EOL> while i < n and code2 >= ord ( char_list [ i ] ) : <EOL> code2 = code2 + <NUM_LIT:1> <EOL> i = i + <NUM_LIT:1> <EOL> result . append ( code1 ) <EOL> result . append ( code2 ) <EOL> return result <EOL> def uppercase_range ( code1 , code2 ) : <EOL> """<STR_LIT>""" <EOL> code3 = max ( code1 , ord ( '<STR_LIT:a>' ) ) <EOL> code4 = min ( code2 , ord ( '<STR_LIT:z>' ) + <NUM_LIT:1> ) <EOL> if code3 < code4 : <EOL> d = ord ( '<STR_LIT:A>' ) - ord ( '<STR_LIT:a>' ) <EOL> return ( code3 + d , code4 + d ) <EOL> else : <EOL> return None <EOL> def lowercase_range ( code1 , code2 ) : <EOL> """<STR_LIT>""" <EOL> code3 = max ( code1 , ord ( '<STR_LIT:A>' ) ) <EOL> code4 = min ( code2 , ord ( '<STR_LIT>' ) + <NUM_LIT:1> ) <EOL> if code3 < code4 : <EOL> d = ord ( '<STR_LIT:a>' ) - ord ( '<STR_LIT:A>' ) <EOL> return ( code3 + d , code4 + d ) <EOL> else : <EOL> return None <EOL> def CodeRanges ( code_list ) : <EOL> """<STR_LIT>""" <EOL> re_list = [ ] <EOL> for i in xrange ( <NUM_LIT:0> , len ( code_list ) , <NUM_LIT:2> ) : <EOL> re_list . append ( CodeRange ( code_list [ i ] , code_list [ i + <NUM_LIT:1> ] ) ) <EOL> return apply ( Alt , tuple ( re_list ) ) <EOL> def CodeRange ( code1 , code2 ) : <EOL> """<STR_LIT>""" <EOL> if code1 <= nl_code < code2 : <EOL> return Alt ( RawCodeRange ( code1 , nl_code ) , <EOL> RawNewline , <EOL> RawCodeRange ( nl_code + <NUM_LIT:1> , code2 ) ) <EOL> else : <EOL> return RawCodeRange ( code1 , code2 ) <EOL> class RE : <EOL> """<STR_LIT>""" <EOL> nullable = <NUM_LIT:1> <EOL> match_nl = <NUM_LIT:1> <EOL> str = None <EOL> def build_machine ( self , machine , initial_state , final_state , <EOL> match_bol , nocase ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" % <EOL> self . __class__ . __name__ ) <EOL> def build_opt ( self , m , initial_state , c ) : <EOL> """<STR_LIT>""" <EOL> s = m . new_state ( ) <EOL> initial_state . link_to ( s ) <EOL> initial_state . add_transition ( c , s ) <EOL> return s <EOL> def __add__ ( self , other ) : <EOL> return Seq ( self , other ) <EOL> def __or__ ( self , other ) : <EOL> return Alt ( self , other ) <EOL> def __str__ ( self ) : <EOL> if self . str : <EOL> return self . str <EOL> else : <EOL> return self . calc_str ( ) <EOL> def check_re ( self , num , value ) : <EOL> if not isinstance ( value , RE ) : <EOL> self . wrong_type ( num , value , "<STR_LIT>" ) <EOL> def check_string ( self , num , value ) : <EOL> if type ( value ) != type ( '<STR_LIT>' ) : <EOL> self . wrong_type ( num , value , "<STR_LIT:string>" ) <EOL> def check_char ( self , num , value ) : <EOL> self . check_string ( num , value ) <EOL> if len ( value ) != <NUM_LIT:1> : <EOL> raise Errors . PlexValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( <EOL> num , self . __class__ . __name__ , repr ( value ) ) ) <EOL> def wrong_type ( self , num , value , expected ) : <EOL> if type ( value ) == types . InstanceType : <EOL> got = "<STR_LIT>" % ( <EOL> value . __class__ . __module__ , value . __class__ . __name__ ) <EOL> else : <EOL> got = type ( value ) . __name__ <EOL> raise Errors . PlexTypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( <EOL> num , self . __class__ . __name__ , expected , got ) ) <EOL> def Char ( c ) : <EOL> """<STR_LIT>""" <EOL> if len ( c ) == <NUM_LIT:1> : <EOL> result = CodeRange ( ord ( c ) , ord ( c ) + <NUM_LIT:1> ) <EOL> else : <EOL> result = SpecialSymbol ( c ) <EOL> result . str = "<STR_LIT>" % repr ( c ) <EOL> return result <EOL> class RawCodeRange ( RE ) : <EOL> """<STR_LIT>""" <EOL> nullable = <NUM_LIT:0> <EOL> match_nl = <NUM_LIT:0> <EOL> range = None <EOL> uppercase_range = None <EOL> lowercase_range = None <EOL> def __init__ ( self , code1 , code2 ) : <EOL> self . range = ( code1 , code2 ) <EOL> self . uppercase_range = uppercase_range ( code1 , code2 ) <EOL> self . lowercase_range = lowercase_range ( code1 , code2 ) <EOL> def build_machine ( self , m , initial_state , final_state , match_bol , nocase ) : <EOL> if match_bol : <EOL> initial_state = self . build_opt ( m , initial_state , BOL ) <EOL> initial_state . add_transition ( self . range , final_state ) <EOL> if nocase : <EOL> if self . uppercase_range : <EOL> initial_state . add_transition ( self . uppercase_range , final_state ) <EOL> if self . lowercase_range : <EOL> initial_state . add_transition ( self . lowercase_range , final_state ) <EOL> def calc_str ( self ) : <EOL> return "<STR_LIT>" % ( self . code1 , self . code2 ) <EOL> class _RawNewline ( RE ) : <EOL> """<STR_LIT>""" <EOL> nullable = <NUM_LIT:0> <EOL> match_nl = <NUM_LIT:1> <EOL> def build_machine ( self , m , initial_state , final_state , match_bol , nocase ) : <EOL> if match_bol : <EOL> initial_state = self . build_opt ( m , initial_state , BOL ) <EOL> s = self . build_opt ( m , initial_state , EOL ) <EOL> s . add_transition ( ( nl_code , nl_code + <NUM_LIT:1> ) , final_state ) <EOL> RawNewline = _RawNewline ( ) <EOL> class SpecialSymbol ( RE ) : <EOL> """<STR_LIT>""" <EOL> nullable = <NUM_LIT:0> <EOL> match_nl = <NUM_LIT:0> <EOL> sym = None <EOL> def __init__ ( self , sym ) : <EOL> self . sym = sym <EOL> def build_machine ( self , m , initial_state , final_state , match_bol , nocase ) : <EOL> if match_bol and self . sym == EOL : <EOL> initial_state = self . build_opt ( m , initial_state , BOL ) <EOL> initial_state . add_transition ( self . sym , final_state ) <EOL> class Seq ( RE ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * re_list ) : <EOL> nullable = <NUM_LIT:1> <EOL> for i in xrange ( len ( re_list ) ) : <EOL> re = re_list [ i ] <EOL> self . check_re ( i , re ) <EOL> nullable = nullable and re . nullable <EOL> self . re_list = re_list <EOL> self . nullable = nullable <EOL> i = len ( re_list ) <EOL> match_nl = <NUM_LIT:0> <EOL> while i : <EOL> i = i - <NUM_LIT:1> <EOL> re = re_list [ i ] <EOL> if re . match_nl : <EOL> match_nl = <NUM_LIT:1> <EOL> break <EOL> if not re . nullable : <EOL> break <EOL> self . match_nl = match_nl <EOL> def build_machine ( self , m , initial_state , final_state , match_bol , nocase ) : <EOL> re_list = self . re_list <EOL> if len ( re_list ) == <NUM_LIT:0> : <EOL> initial_state . link_to ( final_state ) <EOL> else : <EOL> s1 = initial_state <EOL> n = len ( re_list ) <EOL> for i in xrange ( n ) : <EOL> if i < n - <NUM_LIT:1> : <EOL> s2 = m . new_state ( ) <EOL> else : <EOL> s2 = final_state <EOL> re = re_list [ i ] <EOL> re . build_machine ( m , s1 , s2 , match_bol , nocase ) <EOL> s1 = s2 <EOL> match_bol = re . match_nl or ( match_bol and re . nullable ) <EOL> def calc_str ( self ) : <EOL> return "<STR_LIT>" % string . join ( map ( str , self . re_list ) , "<STR_LIT:U+002C>" ) <EOL> class Alt ( RE ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * re_list ) : <EOL> self . re_list = re_list <EOL> nullable = <NUM_LIT:0> <EOL> match_nl = <NUM_LIT:0> <EOL> nullable_res = [ ] <EOL> non_nullable_res = [ ] <EOL> i = <NUM_LIT:1> <EOL> for re in re_list : <EOL> self . check_re ( i , re ) <EOL> if re . nullable : <EOL> nullable_res . append ( re ) <EOL> nullable = <NUM_LIT:1> <EOL> else : <EOL> non_nullable_res . append ( re ) <EOL> if re . match_nl : <EOL> match_nl = <NUM_LIT:1> <EOL> i = i + <NUM_LIT:1> <EOL> self . nullable_res = nullable_res <EOL> self . non_nullable_res = non_nullable_res <EOL> self . nullable = nullable <EOL> self . match_nl = match_nl <EOL> def build_machine ( self , m , initial_state , final_state , match_bol , nocase ) : <EOL> for re in self . nullable_res : <EOL> re . build_machine ( m , initial_state , final_state , match_bol , nocase ) <EOL> if self . non_nullable_res : <EOL> if match_bol : <EOL> initial_state = self . build_opt ( m , initial_state , BOL ) <EOL> for re in self . non_nullable_res : <EOL> re . build_machine ( m , initial_state , final_state , <NUM_LIT:0> , nocase ) <EOL> def calc_str ( self ) : <EOL> return "<STR_LIT>" % string . join ( map ( str , self . re_list ) , "<STR_LIT:U+002C>" ) <EOL> class Rep1 ( RE ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , re ) : <EOL> self . check_re ( <NUM_LIT:1> , re ) <EOL> self . re = re <EOL> self . nullable = re . nullable <EOL> self . match_nl = re . match_nl <EOL> def build_machine ( self , m , initial_state , final_state , match_bol , nocase ) : <EOL> s1 = m . new_state ( ) <EOL> s2 = m . new_state ( ) <EOL> initial_state . link_to ( s1 ) <EOL> self . re . build_machine ( m , s1 , s2 , match_bol or self . re . match_nl , nocase ) <EOL> s2 . link_to ( s1 ) <EOL> s2 . link_to ( final_state ) <EOL> def calc_str ( self ) : <EOL> return "<STR_LIT>" % self . re <EOL> class SwitchCase ( RE ) : <EOL> """<STR_LIT>""" <EOL> re = None <EOL> nocase = None <EOL> def __init__ ( self , re , nocase ) : <EOL> self . re = re <EOL> self . nocase = nocase <EOL> self . nullable = re . nullable <EOL> self . match_nl = re . match_nl <EOL> def build_machine ( self , m , initial_state , final_state , match_bol , nocase ) : <EOL> self . re . build_machine ( m , initial_state , final_state , match_bol , <EOL> self . nocase ) <EOL> def calc_str ( self ) : <EOL> if self . nocase : <EOL> name = "<STR_LIT>" <EOL> else : <EOL> name = "<STR_LIT>" <EOL> return "<STR_LIT>" % ( name , self . re ) <EOL> Empty = Seq ( ) <EOL> Empty . __doc__ = """<STR_LIT>""" <EOL> Empty . str = "<STR_LIT>" <EOL> def Str1 ( s ) : <EOL> """<STR_LIT>""" <EOL> result = apply ( Seq , tuple ( map ( Char , s ) ) ) <EOL> result . str = "<STR_LIT>" % repr ( s ) <EOL> return result <EOL> def Str ( * strs ) : <EOL> """<STR_LIT>""" <EOL> if len ( strs ) == <NUM_LIT:1> : <EOL> return Str1 ( strs [ <NUM_LIT:0> ] ) <EOL> else : <EOL> result = apply ( Alt , tuple ( map ( Str1 , strs ) ) ) <EOL> result . str = "<STR_LIT>" % string . join ( map ( repr , strs ) , "<STR_LIT:U+002C>" ) <EOL> return result <EOL> def Any ( s ) : <EOL> """<STR_LIT>""" <EOL> result = CodeRanges ( chars_to_ranges ( s ) ) <EOL> result . str = "<STR_LIT>" % repr ( s ) <EOL> return result <EOL> def AnyBut ( s ) : <EOL> """<STR_LIT>""" <EOL> ranges = chars_to_ranges ( s ) <EOL> ranges . insert ( <NUM_LIT:0> , - maxint ) <EOL> ranges . append ( maxint ) <EOL> result = CodeRanges ( ranges ) <EOL> result . str = "<STR_LIT>" % repr ( s ) <EOL> return result <EOL> AnyChar = AnyBut ( "<STR_LIT>" ) <EOL> AnyChar . __doc__ = """<STR_LIT>""" <EOL> AnyChar . str = "<STR_LIT>" <EOL> def Range ( s1 , s2 = None ) : <EOL> """<STR_LIT>""" <EOL> if s2 : <EOL> result = CodeRange ( ord ( s1 ) , ord ( s2 ) + <NUM_LIT:1> ) <EOL> result . str = "<STR_LIT>" % ( s1 , s2 ) <EOL> else : <EOL> ranges = [ ] <EOL> for i in range ( <NUM_LIT:0> , len ( s1 ) , <NUM_LIT:2> ) : <EOL> ranges . append ( CodeRange ( ord ( s1 [ i ] ) , ord ( s1 [ i + <NUM_LIT:1> ] ) + <NUM_LIT:1> ) ) <EOL> result = apply ( Alt , tuple ( ranges ) ) <EOL> result . str = "<STR_LIT>" % repr ( s1 ) <EOL> return result <EOL> def Opt ( re ) : <EOL> """<STR_LIT>""" <EOL> result = Alt ( re , Empty ) <EOL> result . str = "<STR_LIT>" % re <EOL> return result <EOL> def Rep ( re ) : <EOL> """<STR_LIT>""" <EOL> result = Opt ( Rep1 ( re ) ) <EOL> result . str = "<STR_LIT>" % re <EOL> return result <EOL> def NoCase ( re ) : <EOL> """<STR_LIT>""" <EOL> return SwitchCase ( re , nocase = <NUM_LIT:1> ) <EOL> def Case ( re ) : <EOL> """<STR_LIT>""" <EOL> return SwitchCase ( re , nocase = <NUM_LIT:0> ) <EOL> Bol = Char ( BOL ) <EOL> Bol . __doc__ = """<STR_LIT>""" <EOL> Bol . str = "<STR_LIT>" <EOL> Eol = Char ( EOL ) <EOL> Eol . __doc__ = """<STR_LIT>""" <EOL> Eol . str = "<STR_LIT>" <EOL> Eof = Char ( EOF ) <EOL> Eof . __doc__ = """<STR_LIT>""" <EOL> Eof . str = "<STR_LIT>" </s>
