compiler/PPCTokenizingConfiguration.st
changeset 534 a949c4fe44df
parent 532 132d7898a2a1
equal deleted inserted replaced
533:666372dbe307 534:a949c4fe44df
     7 	classVariableNames:''
     7 	classVariableNames:''
     8 	poolDictionaries:''
     8 	poolDictionaries:''
     9 	category:'PetitCompiler-Core'
     9 	category:'PetitCompiler-Core'
    10 !
    10 !
    11 
    11 
       
    12 !PPCTokenizingConfiguration methodsFor:'accessing - defaults'!
       
    13 
       
    14 defaultParserSuperclass
       
    15     ^ PPTokenizingCompiledParser
       
    16 
       
    17     "Modified (comment): / 01-09-2015 / 08:48:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    18 ! !
       
    19 
    12 !PPCTokenizingConfiguration methodsFor:'compiling'!
    20 !PPCTokenizingConfiguration methodsFor:'compiling'!
    13 
    21 
    14 buildClass: clazz
    22 invokePhases
    15     |  builder |
       
    16     builder := PPCClassBuilder new.
       
    17     
       
    18     builder compiledClassName: clazz name.
       
    19     builder compiledSuperclass: clazz superclass.
       
    20     builder methodDictionary: clazz methodDictionary.
       
    21     builder constants: clazz constants.
       
    22 
    23 
    23     ^ builder compileClass.	
    24     self runPass: PPCTokenDetector.
    24 !
       
    25 
    25 
    26 invokePhases
    26     context options cacheFirstFollow ifTrue:[
    27     self toPPCIr.
    27         self runPass: PPCCacheFirstFollowPass  
    28     self createTokens.
    28     ].  
    29     self cacheFirstFollow.
    29     self runPass: PPCLL1Visitor.
    30     self createLL1Choices.
    30     context options tokenize ifTrue:[
    31     self tokenize.
    31         self runPass: PPCTokenizingVisitor    
    32     self merge.
    32     ].
    33     self specialize.
    33 
    34     self createRecognizingComponents.
    34     self runPass: PPCMergingVisitor .
    35     self specialize.
    35 
    36     self inline.
    36     context options specialize ifTrue:[
    37     self merge.
    37         self runPass: PPCSpecializingVisitor
    38     self check.	
    38     ].
    39     self cacheFirstFollow.
    39 
    40     self buildParserClazz.
    40     self runPass: PPCRecognizerComponentDetector .
    41     self unmarkConsumeTokensForInline.
    41 
    42     self createFSAs.
    42     context options specialize ifTrue:[
    43     self buildScannerTokens.
    43         self runPass: PPCSpecializingVisitor
    44     self buildScannerScans.	
    44     ].
       
    45     context options inline ifTrue:[
       
    46         self runPass: PPCInliningVisitor     
       
    47     ]. 
       
    48 
       
    49     self runPass: PPCMergingVisitor .
       
    50 
       
    51     self runPass: PPCCheckingVisitor.
       
    52     context options cacheFirstFollow ifTrue:[
       
    53         self runPass: PPCCacheFirstFollowPass  
       
    54     ].  
       
    55     self runPass: PPCTokenizingCodeGenerator.
       
    56     self runPass: PPCFSAVisitor.
       
    57     self runPass: PPCTokenCodeGenerator.
       
    58     self runPass: PPCScannerCodeGenerator.  
       
    59 
    45     self generateScanner.
    60     self generateScanner.
    46     self generateParser.
    61     self generateParser.
       
    62 
       
    63     "Modified: / 04-09-2015 / 10:25:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    47 ! !
    64 ! !
    48 
    65 
    49 !PPCTokenizingConfiguration methodsFor:'error handling'!
       
    50 
       
    51 buildParserClazz
       
    52     self runPass: PPCTokenizingCodeGenerator.
       
    53 
       
    54     "Modified: / 25-08-2015 / 00:07:38 / Jan Vrany <jan.vrany@fit."
       
    55     "Modified: / 26-08-2015 / 22:47:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    56 !
       
    57 
       
    58 buildScannerScans
       
    59     | fsas  generator |
       
    60 
       
    61     "TODO JK: Perhpas write separate visitor for this?"
       
    62     fsas := IdentitySet new.
       
    63     fsas addAll:(ir allNodes 
       
    64                 select:[:node | node hasFsa ]
       
    65                 thenCollect:[:node | node fsa ]).
       
    66     fsas addAll:(ir allNodes 
       
    67                 select:[:node | node hasNextFsa ]
       
    68                 thenCollect:[:node | node nextFsa ]).
       
    69     fsas := fsas reject:[:fsa | fsa hasDistinctRetvals not ].
       
    70     generator := (PPCScannerCodeGenerator new)
       
    71             clazz:context scannerClass;
       
    72             options:context options;
       
    73             yourself.
       
    74     fsas do:[:fsa | 
       
    75         generator generate:fsa
       
    76     ].
       
    77 
       
    78     "Modified: / 25-08-2015 / 00:04:43 / Jan Vrany <jan.vrany@fit."
       
    79     "Modified: / 26-08-2015 / 19:57:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    80 !
       
    81 
       
    82 buildScannerTokens
       
    83     self runPass: PPCTokenCodeGenerator
       
    84 
       
    85     "Modified: / 25-08-2015 / 00:04:46 / Jan Vrany <jan.vrany@fit."
       
    86     "Modified: / 26-08-2015 / 22:53:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    87 !
       
    88 
       
    89 generateParser
       
    90     | parserClass  rootMethod |
       
    91 
       
    92     context options generate ifFalse:[
       
    93         ^ self
       
    94     ].
       
    95     rootMethod := context parserClass propertyAt:#rootMethod.
       
    96     context parserClass name:context options parserName.
       
    97     context parserClass superclass:context options parserSuperclass.
       
    98     parserClass := self buildClass:context parserClass.
       
    99     parserClass startSymbol:rootMethod methodName.
       
   100     self remember:parserClass as:#parser.
       
   101     ir := parserClass new
       
   102 
       
   103     "Modified: / 25-08-2015 / 00:05:49 / Jan Vrany"
       
   104     "Modified: / 26-08-2015 / 19:57:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   105 !
       
   106 
       
   107 generateScanner
       
   108     | scanner |
       
   109 
       
   110     context options generate ifFalse:[
       
   111         ^ self
       
   112     ].
       
   113     context scannerClass name:context options scannerName.
       
   114     context scannerClass superclass:context options scannerSuperclass.
       
   115     scanner := (self buildClass:context scannerClass).
       
   116     context parserClass addConstant:scanner as:#scannerClass.
       
   117     ir := scanner.
       
   118     self remember:scanner as:#scanner
       
   119 
       
   120     "Modified: / 25-08-2015 / 00:06:49 / Jan Vrany"
       
   121     "Modified: / 26-08-2015 / 19:58:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   122 ! !
       
   123 
       
   124 !PPCTokenizingConfiguration methodsFor:'phases'!
       
   125 
       
   126 createFSAs
       
   127     ir := PPCFSAVisitor new
       
   128         idGen: context scannerClass idGen;
       
   129         visit: ir.
       
   130 
       
   131     self remember: (self copyTree: ir) as: #withFSAs
       
   132 
       
   133     "Modified: / 25-08-2015 / 00:07:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   134 !
       
   135 
       
   136 createLL1Choices
       
   137     self flag:'This phase needs revisit and update'.
       
   138     self runPass: PPCLL1Visitor
       
   139 
       
   140     "Modified: / 26-08-2015 / 22:52:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   141 !
       
   142 
       
   143 tokenize
       
   144     "
       
   145         This will try transform the parser into the tokenizing parser"
       
   146     
       
   147     context options tokenize ifFalse:[
       
   148         ^ self
       
   149     ].
       
   150     self runPass: PPCTokenizingVisitor
       
   151 
       
   152     "Modified: / 26-08-2015 / 22:48:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   153 !
       
   154 
       
   155 unmarkConsumeTokensForInline
       
   156     "TODO JK: Hack alert, use visitor, or at leas isTokenConsume"
       
   157     ir allNodesDo: [ :node |
       
   158         node class == PPCTokenConsumeNode ifTrue: [ 
       
   159             node unmarkForInline
       
   160         ]
       
   161     ]
       
   162 ! !
       
   163