islands/JavaParser.st
changeset 387 e2b2ccaa4de6
child 454 a9cd5ea7cc36
equal deleted inserted replaced
386:a409905f7f2d 387:e2b2ccaa4de6
       
     1 "{ Package: 'stx:goodies/petitparser/islands' }"
       
     2 
       
     3 PPCompositeParser subclass:#JavaParser
       
     4 	instanceVariableNames:'javaClass classDef classBody methodDef arguments methodBody
       
     5 		methodName block modifiers classId type throws javaClassIsland
       
     6 		methodModifiers semicolon comment singleLineComment string water'
       
     7 	classVariableNames:''
       
     8 	poolDictionaries:''
       
     9 	category:'PetitIslands-Examples'
       
    10 !
       
    11 
       
    12 JavaParser comment:'A JavaParser is a island parser, that can extract method names from a java file.
       
    13 
       
    14 Instance Variables
       
    15 	arguments:		<Object>
       
    16 	block:		<Object>
       
    17 	classBody:		<Object>
       
    18 	classDef:		<Object>
       
    19 	classId:		<Object>
       
    20 	javaClass:		<Object>
       
    21 	javaClassIsland:		<Object>
       
    22 	methodBody:		<Object>
       
    23 	methodDef:		<Object>
       
    24 	methodModifiers:		<Object>
       
    25 	methodName:		<Object>
       
    26 	modifiers:		<Object>
       
    27 	semicolon:		<Object>
       
    28 	throws:		<Object>
       
    29 	type:		<Object>
       
    30 
       
    31 arguments
       
    32 	- xxxxx
       
    33 
       
    34 block
       
    35 	- xxxxx
       
    36 
       
    37 classBody
       
    38 	- xxxxx
       
    39 
       
    40 classDef
       
    41 	- xxxxx
       
    42 
       
    43 classId
       
    44 	- xxxxx
       
    45 
       
    46 javaClass
       
    47 	- xxxxx
       
    48 
       
    49 javaClassIsland
       
    50 	- xxxxx
       
    51 
       
    52 methodBody
       
    53 	- xxxxx
       
    54 
       
    55 methodDef
       
    56 	- xxxxx
       
    57 
       
    58 methodModifiers
       
    59 	- xxxxx
       
    60 
       
    61 methodName
       
    62 	- xxxxx
       
    63 
       
    64 modifiers
       
    65 	- xxxxx
       
    66 
       
    67 semicolon
       
    68 	- xxxxx
       
    69 
       
    70 throws
       
    71 	- xxxxx
       
    72 
       
    73 type
       
    74 	- xxxxx
       
    75 '
       
    76 !
       
    77 
       
    78 !JavaParser methodsFor:'as yet unclassified'!
       
    79 
       
    80 start
       
    81 	^ javaClassIsland
       
    82 ! !
       
    83 
       
    84 !JavaParser methodsFor:'class'!
       
    85 
       
    86 block
       
    87 	^ (${ asParser,
       
    88 		((block island: water) plus / nil asParser island),
       
    89 	$} asParser) ==> [:tokens | nil ]
       
    90 		
       
    91 !
       
    92 
       
    93 classBody
       
    94 	 ^ 
       
    95 	(${ asParser,
       
    96 		(
       
    97 		((methodDef island: water) ==> [:tokens | tokens second]) plus /
       
    98 		((nil asParser island: water) ==> [ :tokens | OrderedCollection new ])
       
    99 		),
       
   100 	$} asParser) ==> [:tokens | tokens second select: [:e | e isNil not ]]
       
   101 !
       
   102 
       
   103 classDef
       
   104 	^ modifiers trim, 'class' asParser, classId trim ==> [:tokens | tokens third ]
       
   105 !
       
   106 
       
   107 classId
       
   108 	^ (#uppercase asParser, (#letter asParser / #digit asParser) star) flatten 
       
   109 !
       
   110 
       
   111 javaClass
       
   112 	^ classDef, ((classBody island:water) ==> [:tokens | tokens second ])
       
   113 !
       
   114 
       
   115 javaClassIsland
       
   116 	^ (javaClass island: water) ==> [:tokens | tokens second]
       
   117 !
       
   118 
       
   119 methodModifiers
       
   120 	^( ('public' asParser / 'private' asParser / 'protected' asParser) optional, 
       
   121 	  'static' asParser trim optional, 
       
   122 	  'final' asParser trim optional,
       
   123 	  'abstract' asParser trim optional,
       
   124 	  'synchronized' asParser trim optional,	
       
   125 	  'native' asParser trim optional) ==> [ :tokens | nil ]
       
   126 !
       
   127 
       
   128 modifiers
       
   129 	^ ('public' asParser / 'private' asParser), 'final' asParser trim optional, 'abstract' asParser trim optional
       
   130 ! !
       
   131 
       
   132 !JavaParser methodsFor:'comments and strings'!
       
   133 
       
   134 comment
       
   135 	| end |
       
   136 	end := '*/' asParser.
       
   137 	^ ('/*' asParser, (#any asParser starLazy: end), end)
       
   138 !
       
   139 
       
   140 singleLineComment
       
   141 	| end |
       
   142 	end := #newline asParser.
       
   143 	^ ('//' asParser, (#any asParser starLazy: end), end)
       
   144 !
       
   145 
       
   146 string
       
   147 	| end |
       
   148 	end := $" asParser.
       
   149 	^ ($" asParser, (#any asParser starLazy: end), end)
       
   150 		name: 'string';
       
   151 		yourself.
       
   152 ! !
       
   153 
       
   154 !JavaParser methodsFor:'context'!
       
   155 
       
   156 updateContext: aPPContext
       
   157 	super updateContext: aPPContext.
       
   158 "	aPPContext globalAt: #waterObjects put: (OrderedCollection 
       
   159 			with: self comment 
       
   160 			with: self singleLineComment
       
   161 			with: self string
       
   162 			with: self block)."
       
   163 ! !
       
   164 
       
   165 !JavaParser methodsFor:'method'!
       
   166 
       
   167 arguments
       
   168 	^ $( asParser, nil asParser island,  $) asParser trim
       
   169 !
       
   170 
       
   171 methodBody 
       
   172 	^ semicolon / block
       
   173 !
       
   174 
       
   175 methodDef
       
   176 	^ methodModifiers, (type island: water), (methodName island:water), (arguments island:water), methodBody ==> [:tokens | tokens third second ]
       
   177 !
       
   178 
       
   179 methodName
       
   180 	^ (#letter asParser, (#letter asParser / #digit asParser) star) flatten 
       
   181 !
       
   182 
       
   183 semicolon
       
   184 	^ ';' asParser
       
   185 !
       
   186 
       
   187 throws
       
   188 	^ 'throws' asParser trim, type trim, ($, asParser, type trim) star
       
   189 !
       
   190 
       
   191 type
       
   192 	^ (#letter asParser, (#letter asParser / #digit asParser) star) flatten 
       
   193 !
       
   194 
       
   195 water
       
   196 	"
       
   197 		This will allow to skip over
       
   198 			- Strings, 
       
   199 			- Comments 
       
   200 			- and blocks 
       
   201 		when parsing water. This way, comments and strings cannot confuse the result.
       
   202 	"
       
   203 
       
   204 	^ comment / string / singleLineComment / block / #any asParser
       
   205 ! !
       
   206