JavaParser.st
author Stefan Vogel <sv@exept.de>
Tue, 08 Nov 2005 17:49:17 +0100
changeset 2121 7b06266d8249
parent 749 e898eaeff091
child 2152 1cbdfbcc685c
permissions -rw-r--r--
/tmp/cvsyRpZ5v

"
 COPYRIGHT (c) 1996-2011 by Claus Gittinger
 COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
                            SWING Research Group, Czech Technical University in Prague

 Parts of the code written by Claus Gittinger are under following
 license:

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.

 Parts of the code written at SWING Reasearch Group [1] are MIT licensed:

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the 'Software'), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.

 [1] Code written at SWING Research Group contain a signature
     of one of the above copright owners.
"
"{ Package: 'stx:libjava' }"

PPCompositeParser subclass:#JavaParser
	instanceVariableNames:'classDecl'
	classVariableNames:''
	poolDictionaries:''
	category:'Languages-Java-Parser'
!

PPParser subclass:#BlockParser
	instanceVariableNames:'openBlockChar closeBlockChar innerBlockCount'
	classVariableNames:''
	poolDictionaries:''
	privateIn:JavaParser
!

PPParser subclass:#CommentParser
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	privateIn:JavaParser
!

PPParser subclass:#MultilineCommentParser
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	privateIn:JavaParser
!

PPJavaNode subclass:#PPBlockNode
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	privateIn:JavaParser::MultilineCommentParser
!

PPParser subclass:#LineNumberParser
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	privateIn:JavaParser
!

ReadStream subclass:#LineNumberStream
	instanceVariableNames:'eolPositions lastPosition previousWasCR'
	classVariableNames:'CR LF'
	poolDictionaries:''
	privateIn:JavaParser
!

JavaParser::MultilineCommentParser subclass:#JavaDocParser
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	privateIn:JavaParser
!

PPParser subclass:#StatementParser
	instanceVariableNames:'brackets'
	classVariableNames:''
	poolDictionaries:''
	privateIn:JavaParser
!

!JavaParser class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1996-2011 by Claus Gittinger
 COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
                            SWING Research Group, Czech Technical University in Prague

 Parts of the code written by Claus Gittinger are under following
 license:

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.

 Parts of the code written at SWING Reasearch Group [1] are MIT licensed:

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the 'Software'), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.

 [1] Code written at SWING Research Group contain a signature
     of one of the above copright owners.

"
! !

!JavaParser class methodsFor:'parsing'!

methodsIn: sourceCode
    ^ (self parse: sourceCode) methods.

    "Created: / 08-01-2011 / 15:42:24 / Jan Kurs <kurs.jan@post.cz>"
!

parse: code
    ^ JavaParseResult from: (super parse: code).

    "Created: / 08-01-2011 / 16:05:01 / Jan Kurs <kurs.jan@post.cz>"
    "Modified: / 07-04-2011 / 22:07:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaParser methodsFor:'accessing'!

lineNumber
    ^ LineNumberParser new.

    "Created: / 29-12-2010 / 22:14:02 / Jan Kurs <kurs.jan@post.cz>"
    "Modified: / 07-04-2011 / 22:05:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

start
	^ self javaFile end.
! !

!JavaParser methodsFor:'grammar'!

annotation
	^ $@ asParser, self identifier, self annotationBlock optional 
!

annotationBlock
        ^ BlockParser new
                openBlockChar: $(;
                closeBlockChar: $);
                yourself;
        trim

    "Modified: / 07-04-2011 / 21:46:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

argument
	^ (self finalKW optional, self type, self threeDotsKW optional, self typeIdentifier) trim
!

argumentList
	^ (self argument separatedBy:  ($, asParser trim) ==> [:token | nil]) ==> [: token | token select: [:each | each notNil ]].
!

block
        ^ BlockParser new trim

    "Modified: / 07-04-2011 / 21:46:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

classBody
        ^ (${ asParser trim, self classContents star, $} asParser trim) 
        foldLeft: [:a :contents :b | contents select: [:each | each notNil ]]

    "Modified: / 30-12-2010 / 11:21:09 / Jan Kurs <kurs.jan@post.cz>"
!

classContents 
        ^       self methodDecl trim / 
                self constructor trim /
                self staticInitializer trim / 
                self comment /
                "Since classes may be nested recursively we have to use this syntax - without self" 
                classDecl trim / 
                "TODO JK: This will cause problems - sooner or later"
                self statement.

    "Modified: / 29-12-2010 / 22:12:11 / Jan Kurs <kurs.jan@post.cz>"
!

classDecl
	^ 	self classModifiers, 
		(self classKW / self interfaceKW), 
		(self identifier, self genericBlock optional) flatten,
		(self extends optional),
		(self implements optional),
		self classBody
!

classModifiers
	^ 	('public' asParser /
		'private' asParser /
		'static' asParser /
		'abstract' asParser /
		'final' asParser 
		
		) trim star
!

comment
        ^ (self oneLineComment / self multilineComment) ==> [ :token | nil ]

    "Modified: / 30-12-2010 / 11:20:56 / Jan Kurs <kurs.jan@post.cz>"
!

constructor
        ^ (     
                self lineNumber,
                self javadoc optional, 
                self methodModifiers optional, 
                self identifier, 
                self methodArguments, 
                self methodBody optional) trim
                        foldLeft: [:startLine :javadoc :modifiers :name :arguments :body | 
                                JavaMethodDeclarationNode new
                                        startLine: startLine;
                                        javadoc: javadoc;
                                        modifiers: modifiers;
                                        retval: nil;
                                        methodName: name;
                                        arguments: arguments
                        ].

    "Modified: / 08-01-2011 / 16:27:25 / Jan Kurs <kurs.jan@post.cz>"
!

extends
	^ self extendsKW, self type
!

fileHeader
	^ self comment star, self package optional, self imports optional, self comment star
!

fullIdentifier
	^ (self identifier separatedBy: $. asParser) trim flatten
	
!

genericBlock
        ^ BlockParser new
                openBlockChar: $<;
                closeBlockChar: $>;
                yourself;
        trim

    "Modified: / 07-04-2011 / 21:46:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

identifier
	^ (#letter asParser, #word asParser star) trim flatten
	
!

implements
	^ self implementsKW, (self type separatedBy: $, asParser trim)
!

import
	^ self comment star, 'import' asParser trim, self statement.
!

imports
	^ self import star
!

javaFile
	^ (self fileHeader, classDecl trim) foldLeft: [:header :c | c ]
!

javadoc
        ^ JavaDocParser  new trim

    "Modified: / 07-04-2011 / 22:06:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

methodArguments
	^ ($( asParser, self argumentList optional, $) asParser) trim foldLeft: [:a :args :b | args].
!

methodBody
	^ self block.
!

methodDecl
        ^ (    
                self lineNumber,
                self javadoc optional, 
                self methodModifiers optional, 
                self genericBlock optional,
                self methodRetval, 
                self identifier, 
                self methodArguments,
                self throwsStatement optional, 
                self methodBody optional) trim
                        foldLeft: [:startLine :javadoc :modifiers :generic :retval :name :arguments :throws :body | 
                                JavaMethodDeclarationNode new
                                        javadoc: javadoc;
                                        modifiers: modifiers;
                                        retval: retval;
                                        methodName: name;
                                        arguments: arguments;
                                        startLine: startLine
                        ].

    "Modified: / 29-12-2010 / 22:47:53 / Jan Kurs <kurs.jan@post.cz>"
!

methodModifiers 
	^ ('public' asParser / 
	   'static' asParser /
	   'abstract' asParser /
	   'private'	asParser /
	   'protected' asParser /
	   'native' asParser /
	   'final' asParser /
	   'synchronized' asParser /
	   self annotation 
	) trim star
!

methodRetval
	^ self type
!

multilineComment
        ^ MultilineCommentParser new trim

    "Modified: / 07-04-2011 / 22:03:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

nameToken
	^ #word asParser 
	
!

oneLineComment
        ^ CommentParser new trim

    "Modified: / 07-04-2011 / 22:03:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

otherClassContent 
	^ self statement.
!

package
	^ 'package' asParser trim, self statement.
!

primitiveType
	^ (	'int' asParser / 
		'byte' asParser /
		'boolean' asParser /
		'float' asParser /
		'double' asParser /
		'char' asParser 
	) trim
!

reference
	^ self fullIdentifier 
!

statement
        ^ StatementParser new trim ==> [: token | nil ].

    "Modified: / 30-12-2010 / 11:20:45 / Jan Kurs <kurs.jan@post.cz>"
    "Modified: / 07-04-2011 / 22:05:13 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

staticInitializer 
        ^ 'static' asParser trim, BlockParser new ==> [:token | nil]

    "Modified: / 07-04-2011 / 21:47:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

throwsStatement 
	^ self throwsKW, (self identifier separatedBy: $, asParser trim)
!

type
	^ 
	(
		('void' asParser / self primitiveType / self reference) trim, 
		 self genericBlock optional,
		'[]' asParser optional
	) 
	flatten 
!

typeIdentifier 
	^ self identifier 
! !

!JavaParser methodsFor:'keywords'!

classKW
	^ 'class' asParser trim
!

extendsKW
	^ 'extends' asParser trim
!

finalKW
	^ 'final' asParser trim
!

implementsKW
	^ 'implements' asParser trim
!

interfaceKW
	^ 'interface' asParser trim
!

threeDotsKW
	^ '...' asParser trim
!

throwsKW
	^ 'throws' asParser trim
!

voidKW
	^ 'void' asParser trim
! !

!JavaParser methodsFor:'parsing'!

parseOn: ppStream
    ^ super parseOn: (LineNumberStream on: ppStream collection).
"/    ^ super parseOn: ppStream.

    "Created: / 29-12-2010 / 22:35:30 / Jan Kurs <kurs.jan@post.cz>"
    "Modified: / 30-12-2010 / 10:01:20 / Jan Kurs <kurs.jan@post.cz>"
    "Modified: / 07-04-2011 / 22:06:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaParser::BlockParser class methodsFor:'documentation'!

version_SVN
    ^ '$Id$'
! !

!JavaParser::BlockParser methodsFor:'accessing'!

closeBlockChar: anObject
	closeBlockChar := anObject
!

openBlockChar: anObject
	openBlockChar := anObject
! !

!JavaParser::BlockParser methodsFor:'initialization'!

initialize
	innerBlockCount := 0.
	openBlockChar := ${.
	closeBlockChar := $}.
! !

!JavaParser::BlockParser methodsFor:'parsing'!

decInnerBlockCount
	innerBlockCount := innerBlockCount - 1.
!

incInnerBlockCount
	innerBlockCount := innerBlockCount + 1.
!

parseLoop: aStream
	| literal |

	aStream atEnd ifFalse: 
	[
		literal := aStream uncheckedPeek.
		literal = openBlockChar  ifTrue: [ self incInnerBlockCount].
		literal = closeBlockChar  ifTrue: [ self decInnerBlockCount].
		aStream next.
		^true
	].
	^false
!

parseOn: aStream
	| literal |
"	self halt.
"	
	(self parseLoop: aStream) ifFalse: 
	[
			^ PPFailure message: 'unexpected end of input' at: aStream position
	].

	self zeroBlockCount ifTrue: [ ^ PPFailure message: ('expected ' copyWith: openBlockChar)  at: aStream position ].
	
	[self zeroBlockCount] whileFalse: [
		(self parseLoop: aStream) ifFalse: 
		[
			^ PPFailure message: 'unexpected end of input' at: aStream position
		]
	].
	^ nil.
!

zeroBlockCount
	^ innerBlockCount = 0
! !

!JavaParser::CommentParser class methodsFor:'documentation'!

version_SVN
    ^ '$Id$'
! !

!JavaParser::CommentParser methodsFor:'parsing'!

parseOn: aStream
	| literal wasStar |
	wasStar := false.
	
	(aStream next: 2) = '//' ifFalse: [
		^ PPFailure message: '// expected' at: aStream position.
	].
	
	[literal := aStream next.
		literal = (Character cr) or: [ aStream atEnd ]
	] whileFalse.
	
	^ nil.
! !

!JavaParser::MultilineCommentParser class methodsFor:'documentation'!

version_SVN
    ^ '$Id$'
! !

!JavaParser::MultilineCommentParser methodsFor:'parsing'!

checkStart: aStream
	^ (aStream next: self startSequence size) = self startSequence 
!

parseOn: aStream
        | literal wasStar line |
        wasStar := false.
        line := aStream lineNumber.

        (self checkStart: aStream) ifFalse:     [
                 ^ PPFailure message: self startSequence, ' expected' at: aStream position.
        ].
        
        [literal := aStream next.
                aStream atEnd ifTrue: 
                [
                        ^ PPFailure message: 'unexpected end of input' at: aStream position.
                ].
        
                wasStar and: [literal = $/]
        ] whileFalse: [
                wasStar := literal = $*
        ].
        ^ PPBlockNode new
            startLine: line

    "Modified: / 29-12-2010 / 21:29:26 / Jan Kurs <kurs.jan@post.cz>"
!

startSequence 
	^ '/*'
! !

!JavaParser::LineNumberParser class methodsFor:'documentation'!

version_SVN
    ^ '$Id$'
! !

!JavaParser::LineNumberParser methodsFor:'parsing'!

parseOn: aStream
    ^ aStream lineNumber.

    "Created: / 29-12-2010 / 22:13:22 / Jan Kurs <kurs.jan@post.cz>"
! !

!JavaParser::LineNumberStream class methodsFor:'documentation'!

version_SVN
    ^ '$Id$'
! !

!JavaParser::LineNumberStream class methodsFor:'initialization'!

initialize
    CR := Character cr.
    Smalltalk isSmalltalkX ifTrue:[
        CR := Character return.
    ].
    LF := Character nl.

    "Created: / 29-12-2010 / 23:11:17 / Jan Kurs <kurs.jan@post.cz>"
! !

!JavaParser::LineNumberStream methodsFor:'accessing'!

lineNumber
        | index start stop pos |
        pos := position.
        pos >= eolPositions last ifTrue: [^eolPositions size].
        start := 1.
        stop := eolPositions size.
        [start + 1 < stop] whileTrue: 
                        [index := (start + stop) // 2.
                        (eolPositions at: index) <= pos 
                                ifTrue: [start := index]
                                ifFalse: [stop := index]].
        ^start

    "Created: / 30-12-2010 / 10:23:27 / Jan Kurs <kurs.jan@post.cz>"
!

next
        | character |
        character := super next.
        position - 1 == lastPosition 
                ifTrue: 
                        [ 
"/                        self halt.
                        lastPosition := lastPosition + 1.
                        character == CR 
                                ifTrue: 
                                        [eolPositions add: position.
                                        previousWasCR := true]
                                ifFalse: 
                                        [(previousWasCR not and: [character == LF]) 
                                                ifTrue: [eolPositions add: position].
                                        previousWasCR := false]].
        ^character

    "Modified: / 30-12-2010 / 10:55:09 / Jan Kurs <kurs.jan@post.cz>"
!

next: anInteger 
    "Override for positioning purposes"

    | answer num |
    num:= readLimit - position min: anInteger.
    answer := OrderedCollection new: num.

    num timesRepeat: [ 
        answer add: self next.
    ].
    ^ answer asString.


"/        | answer endPosition |
"/        endPosition := position + anInteger min: readLimit.
"/        answer := collection copyFrom: position + 1 to: endPosition.
"/        position := endPosition.
"/        ^ answer

    "Modified: / 30-12-2010 / 10:57:37 / Jan Kurs <kurs.jan@post.cz>"
!

uncheckedPeek
	"An unchecked version of peek that throws an error if we try to peek over the end of the stream, even faster than #peek."

	^ collection at: position + 1
! !

!JavaParser::LineNumberStream methodsFor:'converting'!

asPetitStream
	^ self
! !

!JavaParser::LineNumberStream methodsFor:'initialization'!

initialize
        eolPositions := OrderedCollection with: ZeroPosition.
        lastPosition := ZeroPosition.
        previousWasCR := false

    "Created: / 30-12-2010 / 10:22:41 / Jan Kurs <kurs.jan@post.cz>"
!

on: aCollection
    super on: aCollection.
    eolPositions := OrderedCollection with: ZeroPosition.
    lastPosition := ZeroPosition.
    previousWasCR := false

    "Created: / 30-12-2010 / 10:26:17 / Jan Kurs <kurs.jan@post.cz>"
! !

!JavaParser::LineNumberStream methodsFor:'printing'!

printOn: aStream
	collection isString
		ifFalse: [ ^ super printOn: aStream ].
	aStream
		nextPutAll: (collection copyFrom: 1 to: position);
		nextPutAll: '·';
		nextPutAll: (collection copyFrom: position + 1 to: readLimit)
! !

!JavaParser::JavaDocParser class methodsFor:'documentation'!

version_SVN
    ^ '$Id$'
! !

!JavaParser::JavaDocParser methodsFor:'parsing'!

parseOn: aStream
    | blockNode |
    blockNode := super parseOn: aStream.

    blockNode isPetitFailure ifTrue:    [
        ^ blockNode.    
    ].

    ^ JavadocDeclarationNode new
        startLine: blockNode startLine.

    "Created: / 29-12-2010 / 21:22:42 / Jan Kurs <kurs.jan@post.cz>"
!

startSequence 
	^ '/**'
! !

!JavaParser::StatementParser class methodsFor:'documentation'!

version_SVN
    ^ '$Id$'
! !

!JavaParser::StatementParser methodsFor:'parsing'!

parseOn: aStream
	| literal |
	[literal = $;] whileFalse: [
		aStream atEnd ifFalse: 
		[
			literal := aStream uncheckedPeek.
			aStream next.
		]
		ifTrue: 
		[
			^ PPFailure message: 'unexpected end of input' at: aStream position.
		].
	
		"TODO JK: This will cause problems - sooner or later"
		(literal = $}) ifTrue: [ ^
			PPFailure message: 'cannot parser $}' at: aStream position.
		].
	].
	^ nil
! !

!JavaParser class methodsFor:'documentation'!

version_SVN
    ^ '$Id$'
! !

JavaParser::LineNumberStream initialize!