compiler/tests/extras/PPCLRPParser.st
changeset 516 3b81c9e53352
parent 511 527038bc8edf
parent 515 b5316ef15274
child 529 439c4057517f
equal deleted inserted replaced
514:46dd1237b20a 516:3b81c9e53352
     1 "{ Package: 'stx:goodies/petitparser/compiler/tests/extras' }"
     1 "{ Package: 'stx:goodies/petitparser/compiler/tests/extras' }"
     2 
     2 
     3 "{ NameSpace: Smalltalk }"
     3 "{ NameSpace: Smalltalk }"
     4 
     4 
     5 PPCompositeParser subclass:#PPCLRPParser
     5 PPCompositeParser subclass:#PPCLRPParser
     6 	instanceVariableNames:'program variable block bra ket identifier machine body event
     6 	instanceVariableNames:'program variable smalltalkBlock bra ket identifier machine body
     7 		transition epsilon wildcard state onentry running onexit comment
     7 		event transition epsilon wildcard state onentry running onexit
     8 		lineTerminator statebody spawn integer errorNode success failed
     8 		comment lineTerminator statebody spawn integer errorNode success
     9 		lastError styler timeoutIdentifier timeoutInteger endOfComment'
     9 		failed lastError styler timeoutIdentifier timeoutInteger
       
    10 		endOfComment'
    10 	classVariableNames:''
    11 	classVariableNames:''
    11 	poolDictionaries:''
    12 	poolDictionaries:''
    12 	category:'PetitCompiler-Extras-Tests-LRP'
    13 	category:'PetitCompiler-Extras-Tests-LRP'
    13 !
    14 !
    14 
    15 
   104                 PPCLRPErrorNode new start: tokens first start stop: tokens last stop; yourself.
   105                 PPCLRPErrorNode new start: tokens first start stop: tokens last stop; yourself.
   105             ]
   106             ]
   106 !
   107 !
   107 
   108 
   108 event
   109 event
   109     ^ (bra, 'event' asParser trim, identifier, block,  ket)  
   110     ^ (bra, 'event' asParser trim, identifier, smalltalkBlock,  ket)  
   110         ==> [:tokens | | ident |
   111         ==> [:tokens | | ident |
   111                 ident := (tokens at: 3).
   112                 ident := (tokens at: 3).
   112                 (PPCLRPEvent named: ident inputValue 
   113                 (PPCLRPEvent named: ident inputValue 
   113                      trigger: (tokens  at: 4))
   114                      trigger: (tokens  at: 4))
   114                 start: tokens first start stop: tokens last stop;
   115                 start: tokens first start stop: tokens last stop;
   116                 yourself.
   117                 yourself.
   117             ]
   118             ]
   118 !
   119 !
   119 
   120 
   120 integer 
   121 integer 
   121     ^(#digit asParser) plus flatten trim token
   122     ^(#digit asParser) plus  token trim
   122 !
   123 !
   123 
   124 
   124 machine
   125 machine
   125     ^(bra , 'machine' asParser trim , identifier , body , ket) 
   126     ^(bra , 'machine' asParser trim , identifier , body , ket) 
   126         ==> [:tokens | | ident bod stop |
   127         ==> [:tokens | | ident bod stop |
   127                 ident := (tokens at: 3).
   128                 ident := (tokens at: 3).
   128                 bod := (tokens at: 4).
   129                 bod := (tokens at: 4).
   129                 bod isEmpty
   130                 bod isEmpty
   130                     ifTrue: [ stop := tokens last stop - 1 ]
   131                     ifTrue: [ stop := tokens last stop - 1 ]
   131                  	ifFalse: [ stop := (bod at: 1) start - 1 ].
   132                  		ifFalse: [ stop := (bod at: 1) start - 1 ].
   132                 (PPCLRPMachine name: ident inputValue body: bod)
   133                 (PPCLRPMachine name: ident inputValue body: bod)
   133                     start: (tokens first start) stop: (tokens last stop);
   134                     start: (tokens first start) stop: (tokens last stop);
   134                     nameRange: (ident start to: stop);
   135                     nameRange: (ident start to: stop);
   135                     yourself.
   136                     yourself.
   136             ]
   137             ]
   137 !
   138 !
   138 
   139 
   139 onentry
   140 onentry
   140     ^ (bra, 'onentry' asParser trim, (block/spawn) ,  ket ) 
   141     ^ (bra, 'onentry' asParser trim, (smalltalkBlock/spawn) ,  ket ) 
   141         ==> [:tokens | 
   142         ==> [:tokens | 
   142                 (PPCLRPOnEntry block: (tokens at: 3))
   143                 (PPCLRPOnEntry block: (tokens at: 3))
   143                 start: (tokens first start) stop: (tokens last stop);
   144                 start: (tokens first start) stop: (tokens last stop);
   144                 keywordEnd: (tokens at: 3) start -1;
   145                 keywordEnd: (tokens at: 3) start -1;
   145                 yourself.
   146                 yourself.
   146             ]
   147             ]
   147 !
   148 !
   148 
   149 
   149 onexit
   150 onexit
   150     ^ (bra, 'onexit' asParser trim, (block/spawn),  ket)  
   151     ^ (bra, 'onexit' asParser trim, (smalltalkBlock/spawn),  ket)  
   151         ==> [:tokens | 
   152         ==> [:tokens | 
   152                 (PPCLRPOnExit block: (tokens at: 3))
   153                 (PPCLRPOnExit block: (tokens at: 3))
   153                 start: (tokens first start) stop: (tokens last stop);
   154                 start: (tokens first start) stop: (tokens last stop);
   154                 keywordEnd: (tokens at: 3) start -1;
   155                 keywordEnd: (tokens at: 3) start -1;
   155                 yourself.
   156                 yourself.
   159 program
   160 program
   160     ^ (variable / machine / comment / spawn / errorNode) star
   161     ^ (variable / machine / comment / spawn / errorNode) star
   161 !
   162 !
   162 
   163 
   163 running
   164 running
   164     ^ (bra, 'running' asParser trim, (block/spawn),  ket)  
   165     ^ (bra, 'running' asParser trim, (smalltalkBlock/spawn),  ket)  
   165         ==> [:tokens | 
   166         ==> [:tokens | 
   166                 (PPCLRPRunning block: (tokens at: 3))
   167                 (PPCLRPRunning block: (tokens at: 3))
   167                 start: (tokens first start) stop: (tokens last stop);
   168                 start: (tokens first start) stop: (tokens last stop);
   168                 keywordEnd: (tokens at: 3) start -1;
   169                 keywordEnd: (tokens at: 3) start -1;
   169                 yourself.
   170                 yourself.
   172 
   173 
   173 spawn
   174 spawn
   174     ^(bra , 'spawn' asParser trim , identifier , identifier , ket) 
   175     ^(bra , 'spawn' asParser trim , identifier , identifier , ket) 
   175         ==> [ :tokens |  
   176         ==> [ :tokens |  
   176                 (PPCLRPSpawn
   177                 (PPCLRPSpawn
   177                     machine: (tokens at: 3) parsedValue
   178                     machine: (tokens at: 3) inputValue
   178                     state: (tokens at: 4) parsedValue)
   179                     state: (tokens at: 4) inputValue)
   179                 start: (tokens first start) stop: (tokens last stop);
   180                 start: (tokens first start) stop: (tokens last stop);
   180                 nameRange: ((tokens at: 3) start to: (tokens at: 4) stop)
   181                 nameRange: ((tokens at: 3) start to: (tokens at: 4) stop)
   181                 yourself.
   182                 yourself.
   182             ]
   183             ]
   183         
   184         
   200     ^(onentry / running / onexit / machine / comment / errorNode) star
   201     ^(onentry / running / onexit / machine / comment / errorNode) star
   201         
   202         
   202 !
   203 !
   203 
   204 
   204 variable
   205 variable
   205     ^ (bra , 'var' asParser trim , identifier , ':=' asParser trim , block , ket) 
   206     ^ (bra , 'var' asParser trim , identifier , ':=' asParser trim , smalltalkBlock , ket) 
   206         ==> [ :tokens | |ident|
   207         ==> [ :tokens | |ident|
   207             ident := (tokens at: 3).
   208             ident := (tokens at: 3).
   208             (PPCLRPVariable name: ident inputValue value: (tokens at: 5))
   209             (PPCLRPVariable name: ident inputValue value: (tokens at: 5))
   209                 start: (tokens first start) stop: (tokens last stop);
   210                 start: (tokens first start) stop: (tokens last stop);
   210                 nameRange: (ident start to: ident stop);
   211                 nameRange: (ident start to: ident stop);
   213 ! !
   214 ! !
   214 
   215 
   215 !PPCLRPParser methodsFor:'grammar-comments'!
   216 !PPCLRPParser methodsFor:'grammar-comments'!
   216 
   217 
   217 comment
   218 comment
   218     ^ ( $; asParser token , (endOfComment negate star) flatten,  endOfComment token) trim 
   219     ^ ($; asParser, (endOfComment negate star),  endOfComment) token trim 
   219     ==> [ :tokens |  |text|
   220     ==> [ :token |  |text|
   220             text := tokens  at: 2.
   221             text := token inputValue.
   221                 (PPCLRPComment text: (text copyFrom: 1 to: text size -1))
   222                 (PPCLRPComment text: (text copyFrom: 1 to: text size -1) trim)
   222                 start: (tokens first start) stop: (tokens last stop);
   223                 start: (token start) stop: (token stop);
   223                 yourself.
   224                 yourself.
   224         ]
   225         ]
   225 !
   226 !
   226 
   227 
   227 endOfComment
   228 endOfComment
   233     ^ (Character lf asParser) / (Character cr asParser , (Character lf asParser ) optional )
   234     ^ (Character lf asParser) / (Character cr asParser , (Character lf asParser ) optional )
   234 ! !
   235 ! !
   235 
   236 
   236 !PPCLRPParser methodsFor:'grammar-common'!
   237 !PPCLRPParser methodsFor:'grammar-common'!
   237 
   238 
   238 block
       
   239     ^PPSmalltalkParser new block
       
   240 !
       
   241 
       
   242 bra
   239 bra
   243     ^ $( asParser token trim
   240     ^ $( asParser token trim
   244 !
   241 !
   245 
   242 
   246 identifier 
   243 identifier 
   247     ^(#letter asParser , (#word asParser /  $_ asParser) star) flatten token trim
   244     ^(#letter asParser , (#word asParser /  $_ asParser) star) token trim
   248 !
   245 !
   249 
   246 
   250 ket
   247 ket
   251     ^ $) asParser token trim
   248     ^ $) asParser token trim
       
   249 !
       
   250 
       
   251 smalltalkBlock
       
   252     ^PPSmalltalkParser new productionAt: #block
   252 ! !
   253 ! !
   253 
   254 
   254 !PPCLRPParser methodsFor:'grammar-transitions'!
   255 !PPCLRPParser methodsFor:'grammar-transitions'!
   255 
   256 
   256 epsilon
   257 epsilon