devtools/PPPrinter.st
changeset 123 353f9f08be3f
equal deleted inserted replaced
122:5ea40d035e02 123:353f9f08be3f
       
     1 "{ Package: 'stx:goodies/petitparser/devtools' }"
       
     2 
       
     3 PPParserVisitor subclass:#PPPrinter
       
     4 	instanceVariableNames:'stream root'
       
     5 	classVariableNames:''
       
     6 	poolDictionaries:''
       
     7 	category:'PetitParser-Utils'
       
     8 !
       
     9 
       
    10 
       
    11 !PPPrinter class methodsFor:'instance creation'!
       
    12 
       
    13 new
       
    14     "return an initialized instance"
       
    15 
       
    16     ^ self basicNew initialize.
       
    17 ! !
       
    18 
       
    19 !PPPrinter class methodsFor:'printing'!
       
    20 
       
    21 print: parser
       
    22     ^self new print: parser.
       
    23 
       
    24     "Created: / 11-01-2013 / 09:51:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    25 ! !
       
    26 
       
    27 !PPPrinter methodsFor:'accessing'!
       
    28 
       
    29 stream
       
    30     ^ stream
       
    31 !
       
    32 
       
    33 stream:something
       
    34     stream := something.
       
    35 ! !
       
    36 
       
    37 !PPPrinter methodsFor:'initialization'!
       
    38 
       
    39 initialize
       
    40     stream := String new writeStream.
       
    41     root := nil.
       
    42 
       
    43     "Modified: / 11-01-2013 / 09:21:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    44 ! !
       
    45 
       
    46 !PPPrinter methodsFor:'printing'!
       
    47 
       
    48 print: parser
       
    49     self visit: parser.
       
    50     ^stream contents
       
    51 
       
    52     "Created: / 11-01-2013 / 09:51:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    53 ! !
       
    54 
       
    55 !PPPrinter methodsFor:'visiting'!
       
    56 
       
    57 visit:anObject 
       
    58     "visit anObject (visitor pattern).
       
    59      The object should call back one of my visitXXXX methods."
       
    60 
       
    61     anObject name notNil ifTrue:[
       
    62         stream nextPutAll: anObject name.
       
    63         root isNil ifTrue:[
       
    64             stream cr; cr; tab.
       
    65             stream nextPut: $^.
       
    66             root := anObject.
       
    67             anObject acceptVisitor:self.
       
    68         ].
       
    69         ^self
       
    70     ].
       
    71     stream nextPut:$(.
       
    72     anObject acceptVisitor:self.
       
    73     stream nextPut:$)
       
    74 
       
    75     "Created: / 11-01-2013 / 09:17:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    76 !
       
    77 
       
    78 visitPPChoiceParser:anObject 
       
    79     "dispatched back from the visited pPChoiceParser-object (visitor pattern)"
       
    80 
       
    81     "fall back to general object-case - please change as required"
       
    82 
       
    83     root == anObject ifTrue:[stream space].
       
    84 
       
    85     anObject children 
       
    86         do: [:parser|self visit: parser. root == anObject ifTrue:[stream cr; tab] ifFalse:[stream space]]
       
    87         separatedBy:[stream nextPutAll: '/ '].
       
    88 
       
    89     "Created: / 11-01-2013 / 09:18:59 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    90 !
       
    91 
       
    92 visitPPEpsilonParser:anObject 
       
    93     stream nextPutAll: 'nil asParser'
       
    94 
       
    95     "Created: / 11-01-2013 / 09:34:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    96 !
       
    97 
       
    98 visitPPLiteralObjectParser:anObject 
       
    99     self visitPPLiteralParser: anObject
       
   100 
       
   101     "Created: / 11-01-2013 / 09:29:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   102 !
       
   103 
       
   104 visitPPLiteralParser:anObject 
       
   105     anObject literal storeOn: stream.
       
   106     stream nextPutAll: ' asParser'
       
   107 
       
   108     "Created: / 11-01-2013 / 09:29:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   109 !
       
   110 
       
   111 visitPPLiteralSequenceParser:anObject 
       
   112     self visitPPLiteralParser: anObject
       
   113 
       
   114     "Created: / 11-01-2013 / 09:29:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   115 !
       
   116 
       
   117 visitPPOptionalParser:anObject 
       
   118     self visit: anObject children anElement.
       
   119     stream nextPutAll: ' optional'.
       
   120 
       
   121     "Created: / 11-01-2013 / 09:27:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   122 !
       
   123 
       
   124 visitPPPredicateObjectParser:anObject 
       
   125     stream nextPutAll: '(self error: ''Cannot print predicate object parsers'')'
       
   126 
       
   127     "Created: / 11-01-2013 / 09:32:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   128 !
       
   129 
       
   130 visitPPRepeatingParser:anObject 
       
   131     self visit: anObject children anElement.
       
   132     (anObject min == 0 and:[anObject max = SmallInteger maxVal]) ifTrue:[
       
   133         stream nextPutAll:' star'.
       
   134         ^self.
       
   135     ].
       
   136     (anObject min == 1 and:[anObject max = SmallInteger maxVal]) ifTrue:[
       
   137         stream nextPutAll:' plus'.
       
   138         ^self.
       
   139     ].
       
   140 
       
   141     stream 
       
   142         nextPutAll:' min: ';
       
   143         nextPutAll: anObject min printString;
       
   144         nextPutAll:' max: ';
       
   145         nextPutAll: anObject max printString
       
   146 
       
   147     "Created: / 11-01-2013 / 09:27:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   148 !
       
   149 
       
   150 visitPPSequenceParser:anObject 
       
   151     "dispatched back from the visited pPSequenceParser-object (visitor pattern)"
       
   152 
       
   153     anObject children 
       
   154         do: [:parser|self visit: parser]
       
   155         separatedBy:[stream nextPutAll: ' , '].
       
   156 
       
   157     "Created: / 11-01-2013 / 09:18:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   158 !
       
   159 
       
   160 visitPPUnresolvedParser:anObject 
       
   161     stream nextPutAll: '(self error: ''Unresolved'')'
       
   162 
       
   163     "Created: / 11-01-2013 / 09:33:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   164 ! !
       
   165 
       
   166 !PPPrinter class methodsFor:'documentation'!
       
   167 
       
   168 version
       
   169     ^ '$Header: /cvs/stx/stx/goodies/petitparser/devtools/PPPrinter.st,v 1.1 2013-01-11 09:57:40 vrany Exp $'
       
   170 !
       
   171 
       
   172 version_CVS
       
   173     ^ '$Header: /cvs/stx/stx/goodies/petitparser/devtools/PPPrinter.st,v 1.1 2013-01-11 09:57:40 vrany Exp $'
       
   174 ! !