<s> import py <EOL> import os <EOL> import glob <EOL> import tokenize <EOL> import token <EOL> import StringIO <EOL> from pypy . interpreter . pyparser . metaparser import ParserGenerator , PgenError <EOL> from pypy . interpreter . pyparser . pygram import PythonGrammar <EOL> from pypy . interpreter . pyparser import parser <EOL> class MyGrammar ( parser . Grammar ) : <EOL> TOKENS = token . __dict__ <EOL> OPERATOR_MAP = { <EOL> "<STR_LIT:+>" : token . OP , <EOL> "<STR_LIT:->" : token . OP , <EOL> } <EOL> KEYWORD_TOKEN = token . NAME <EOL> class TestParserGenerator : <EOL> def gram_for ( self , grammar_source ) : <EOL> p = ParserGenerator ( grammar_source + "<STR_LIT:\n>" ) <EOL> return p . build_grammar ( MyGrammar ) <EOL> def test_multiple_rules ( self ) : <EOL> g = self . gram_for ( "<STR_LIT>" ) <EOL> assert len ( g . dfas ) == <NUM_LIT:2> <EOL> assert g . start == g . symbol_ids [ "<STR_LIT:foo>" ] <EOL> def test_simple ( self ) : <EOL> g = self . gram_for ( "<STR_LIT>" ) <EOL> assert len ( g . dfas ) == <NUM_LIT:1> <EOL> eval_sym = g . symbol_ids [ "<STR_LIT>" ] <EOL> assert eval_sym in g . dfas <EOL> assert g . start == eval_sym <EOL> states , first = g . dfas [ eval_sym ] <EOL> assert states == [ ( [ ( <NUM_LIT:1> , <NUM_LIT:1> ) ] , False ) , ( [ ] , True ) ] <EOL> assert g . labels [ <NUM_LIT:0> ] == <NUM_LIT:0> <EOL> def test_load_python_grammars ( self ) : <EOL> gram_pat = os . path . join ( os . path . dirname ( __file__ ) , "<STR_LIT:..>" , "<STR_LIT:data>" , <EOL> "<STR_LIT>" ) <EOL> for gram_file in glob . glob ( gram_pat ) : <EOL> fp = open ( gram_file , "<STR_LIT:r>" ) <EOL> try : <EOL> ParserGenerator ( fp . read ( ) ) . build_grammar ( PythonGrammar ) <EOL> finally : <EOL> fp . close ( ) <EOL> def test_items ( self ) : <EOL> g = self . gram_for ( "<STR_LIT>" ) <EOL> assert len ( g . dfas ) == <NUM_LIT:1> <EOL> states = g . dfas [ g . symbol_ids [ "<STR_LIT:foo>" ] ] [ <NUM_LIT:0> ] <EOL> last = states [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> for state in states [ <NUM_LIT:1> : - <NUM_LIT:1> ] : <EOL> assert last < state [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> last = state [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> def test_alternatives ( self ) : <EOL> g = self . gram_for ( "<STR_LIT>" ) <EOL> assert len ( g . dfas ) == <NUM_LIT:1> <EOL> def test_optional ( self ) : <EOL> g = self . gram_for ( "<STR_LIT>" ) <EOL> def test_grouping ( self ) : <EOL> g = self . gram_for ( "<STR_LIT>" ) <EOL> def test_keyword ( self ) : <EOL> g = self . gram_for ( "<STR_LIT>" ) <EOL> assert len ( g . keyword_ids ) == <NUM_LIT:2> <EOL> assert len ( g . token_ids ) == <NUM_LIT:0> <EOL> def test_token ( self ) : <EOL> g = self . gram_for ( "<STR_LIT>" ) <EOL> assert len ( g . token_ids ) == <NUM_LIT:1> <EOL> def test_operator ( self ) : <EOL> g = self . gram_for ( "<STR_LIT>" ) <EOL> assert len ( g . keyword_ids ) == <NUM_LIT:0> <EOL> assert len ( g . token_ids ) == <NUM_LIT:2> <EOL> exc = py . test . raises ( PgenError , self . gram_for , "<STR_LIT>" ) . value <EOL> assert str ( exc ) == "<STR_LIT>" <EOL> def test_symbol ( self ) : <EOL> g = self . gram_for ( "<STR_LIT>" ) <EOL> assert len ( g . dfas ) == <NUM_LIT:2> <EOL> assert len ( g . labels ) == <NUM_LIT:3> <EOL> exc = py . test . raises ( PgenError , self . gram_for , "<STR_LIT>" ) . value <EOL> assert str ( exc ) == "<STR_LIT>" <EOL> def test_repeaters ( self ) : <EOL> g1 = self . gram_for ( "<STR_LIT>" ) <EOL> g2 = self . gram_for ( "<STR_LIT>" ) <EOL> assert g1 . dfas != g2 . dfas <EOL> g = self . gram_for ( "<STR_LIT>" ) <EOL> g = self . gram_for ( "<STR_LIT>" ) <EOL> def test_error ( self ) : <EOL> exc = py . test . raises ( PgenError , self . gram_for , "<STR_LIT>" ) . value <EOL> assert str ( exc ) == "<STR_LIT>" <EOL> assert exc . location == ( ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:1> , <NUM_LIT:3> ) , "<STR_LIT>" ) <EOL> exc = py . test . raises ( PgenError , self . gram_for , "<STR_LIT>" ) . value <EOL> assert str ( exc ) == "<STR_LIT>" <EOL> assert exc . location == ( ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:1> , <NUM_LIT:3> ) , "<STR_LIT>" ) <EOL> def test_comments_and_whitespace ( self ) : <EOL> self . gram_for ( "<STR_LIT>" ) </s>
