ó S‚ ?c@s~ddlZddlZddlZddlZddlZddlZdZdZddd„ƒYZdd d„ƒYZ dS( iÿÿÿÿNiitStatecBs#eZdZdZdZd„ZRS(s This class is used as part of a Plex.Lexicon specification to introduce a user-defined state. Constructor: State(name, token_specifications) cCs||_||_dS(N(tnamettokens(tselfRR((s7/usr/lib/python2.7/site-packages/Pyrex/Plex/Lexicons.pyt__init__"s N(t__name__t __module__t__doc__tNoneRRR(((s7/usr/lib/python2.7/site-packages/Pyrex/Plex/Lexicons.pyRstLexiconcBsGeZdZdZdZdddd„Zd„Zd„Zd„Z RS(s! Lexicon(specification) builds a lexical analyser from the given |specification|. The specification consists of a list of specification items. Each specification item may be either: 1) A token definition, which is a tuple: (pattern, action) The |pattern| is a regular axpression built using the constructors defined in the Plex module. The |action| is the action to be performed when this pattern is recognised (see below). 2) A state definition: State(name, tokens) where |name| is a character string naming the state, and |tokens| is a list of token definitions as above. The meaning and usage of states is described below. Actions ------- The |action| in a token specication may be one of three things: 1) A function, which is called as follows: function(scanner, text) where |scanner| is the relevant Scanner instance, and |text| is the matched text. If the function returns anything other than None, that value is returned as the value of the token. If it returns None, scanning continues as if the IGNORE action were specified (see below). 2) One of the following special actions: IGNORE means that the recognised characters will be treated as white space and ignored. Scanning will continue until the next non-ignored token is recognised before returning. TEXT causes the scanned text itself to be returned as the value of the token. 3) Any other value, which is returned as the value of the token. States ------ At any given time, the scanner is in one of a number of states. Associated with each state is a set of possible tokens. When scanning, only tokens associated with the current state are recognised. There is a default state, whose name is the empty string. Token definitions which are not inside any State definition belong to the default state. The initial state of the scanner is the default state. The state can be changed in one of two ways: 1) Using Begin(state_name) as the action of a token. 2) Calling the begin(state_name) method of the Scanner. To change back to the default state, use '' as the state name. icCs<t|ƒtjkr'tjdƒ‚n|rOddlm}d}|ƒ}ntjƒ}|j dƒ} d} x°|D]¨} t | t ƒrÕ|j | j ƒ} x~| j D]&} |j|| | | ƒ| d} q¨Wqwt| ƒtjkr |j|| | | ƒ| d} qwtj| dƒ‚qwW|rL|ƒ}|||}|ƒ}n|ry|d@ry|jdƒ|j|ƒntj|d |d @d ko˜|ƒ}|r¾|ƒ}|||}n|rë|d @rë|jd ƒ|j|ƒn|r/|jd ||ƒ|jd||ƒ|jd|ƒn||_dS(Ns Scanner definition is not a listiÿÿÿÿ(ttimegtis5Expected a token definition (tuple) or State instances ============= NFA =========== tdebugiis ============= DFA =========== sConstructing NFA : %5.2f sConverting to DFA: %5.2f sTOTAL : %5.2f (ttypettypestListTypetErrorstInvalidScannertTimingR tMachinestMachinetnew_initial_statet isinstanceRRRtadd_token_to_machinet TupleTypet InvalidTokentwritetdumptDFAt nfa_to_dfatmachine(RtspecificationsR t debug_flagsttimingsR t total_timettime1tnfatdefault_initial_statet token_numbertspectuser_initial_statettokenttime2ttime3tdfattime4((s7/usr/lib/python2.7/site-packages/Pyrex/Plex/Lexicons.pyRqsT        %  c CsÖy |j|ƒ\}}t|tjƒr3|}n-t|ƒrQtj|ƒ}ntj|ƒ}|jƒ}|j|||ddddƒ|j |d| ƒWn/t j k rÑ} | j d|| fƒ‚nXdS(Nt match_bolitnocaseitprioritysToken number %d: %s( tparse_token_definitionRtActionstActiontcallabletCalltReturnt new_statet build_machinet set_actionRt PlexErrort __class__( RRt initial_statet token_specR&tret action_spectactiont final_statete((s7/usr/lib/python2.7/site-packages/Pyrex/Plex/Lexicons.pyRžs    cCs…t|ƒtjkr'tjdƒ‚nt|ƒdkrKtjdƒ‚n|\}}t|tjƒs{tjdƒ‚n||fS(NsToken definition is not a tupleis)Wrong number of items in token definitionsPattern is not an RE instance( R RRRRtlenRtRegexpstRE(RR=tpatternR@((s7/usr/lib/python2.7/site-packages/Pyrex/Plex/Lexicons.pyR1²s cCs|jj|ƒS(N(Rtget_initial_state(RR((s7/usr/lib/python2.7/site-packages/Pyrex/Plex/Lexicons.pyRG¼sN( RRRRRttablesRRR1RG(((s7/usr/lib/python2.7/site-packages/Pyrex/Plex/Lexicons.pyR &sF-  ((( RR2RRRRDtDUMP_NFAtDUMP_DFARR (((s7/usr/lib/python2.7/site-packages/Pyrex/Plex/Lexicons.pyt s