src/GroovyCompiler.st
branchjk_new_structure
changeset 1387 4c609318f0e5
parent 1385 3426388640da
child 1388 71a9ff13de80
equal deleted inserted replaced
1386:5a5fc5c71e48 1387:4c609318f0e5
    19      as of 1.9.2010
    19      as of 1.9.2010
    20 "
    20 "
    21 "{ Package: 'stx:libjava' }"
    21 "{ Package: 'stx:libjava' }"
    22 
    22 
    23 Object subclass:#GroovyCompiler
    23 Object subclass:#GroovyCompiler
    24 	instanceVariableNames:''
    24 	instanceVariableNames:'requestor'
    25 	classVariableNames:'GroovyClassLoader'
    25 	classVariableNames:'GroovyClassLoader'
    26 	poolDictionaries:''
    26 	poolDictionaries:''
    27 	category:'Languages-Groovy-Compiler'
    27 	category:'Languages-Groovy-Compiler'
    28 !
    28 !
    29 
    29 
    74 
    74 
    75 compile: source forClass: class inCategory: category notifying: requestor install: doInstall
    75 compile: source forClass: class inCategory: category notifying: requestor install: doInstall
    76 
    76 
    77     "We allways compile whole class"
    77     "We allways compile whole class"
    78 
    78 
    79     ^self compileClass: source.
    79     ^self new
       
    80         requestor: requestor;
       
    81         compileClass: source.
    80 
    82 
    81     "Created: / 21-02-2012 / 11:10:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    83     "Created: / 21-02-2012 / 11:10:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    82 !
    84 !
    83 
    85 
    84 compileClass: source
    86 compileClass: source
    87     ^self new compileClass: source.
    89     ^self new compileClass: source.
    88 
    90 
    89     "Created: / 18-02-2012 / 19:09:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    91     "Created: / 18-02-2012 / 19:09:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    90 ! !
    92 ! !
    91 
    93 
       
    94 !GroovyCompiler methodsFor:'accessing'!
       
    95 
       
    96 requestor
       
    97     ^ requestor
       
    98 !
       
    99 
       
   100 requestor:something
       
   101     requestor := something.
       
   102 ! !
       
   103 
    92 !GroovyCompiler methodsFor:'compiler interface'!
   104 !GroovyCompiler methodsFor:'compiler interface'!
    93 
   105 
    94 compileClass: source
   106 compileClass: source
    95     "Compiles a new Groovy class given the source code"
   107     "Compiles a new Groovy class given the source code"
    96 
   108 
    97     | jclass class |    
   109     | jclass class |    
    98 
   110 
    99     jclass := GroovyClassLoader 
   111     [
   100                 perform: #'parseClass(Ljava/lang/String;)Ljava/lang/Class;'
   112         jclass := GroovyClassLoader 
   101                    with: (Java as_String: source).
   113                     perform: #'parseClass(Ljava/lang/String;)Ljava/lang/Class;'
       
   114                        with: (Java as_String: source).
       
   115     ] on: JavaError do:[:jex|
       
   116         self handleException: jex.
       
   117     ].
   102     jclass isNil ifTrue:[ ^ nil ].
   118     jclass isNil ifTrue:[ ^ nil ].
   103 
   119 
   104     class := JavaVM classForJavaClassObject: jclass.
   120     class := JavaVM classForJavaClassObject: jclass.
   105     class setSource: source.
   121     class setSource: source.
   106     ^class.
   122     ^class.
   107 
   123 
   108     "Created: / 18-02-2012 / 19:12:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   124     "Created: / 18-02-2012 / 19:12:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   125 ! !
       
   126 
       
   127 !GroovyCompiler methodsFor:'error reporting'!
       
   128 
       
   129 error: message line: line from: start to: end
       
   130     "notify requestor of an error - if there is no requestor
       
   131      put it on the transcript. Requestor is typically the CodeView
       
   132      in which the accept/doIt was triggered, or the PositionableStream
       
   133      which does the fileIn. The requestor may decide how to highlight the
       
   134      error (and/or to abort the compile).
       
   135      Return the result passed back by the requestor."
       
   136 
       
   137     |err|
       
   138 
       
   139     Smalltalk isInitialized ifFalse:[
       
   140         Smalltalk isStandAloneDebug ifTrue:[
       
   141             "/ error during startup
       
   142             thisContext fullPrintAll.
       
   143         ]
       
   144     ].
       
   145 
       
   146 
       
   147     "/ backward compatibility - will vanish eventually (use a handler, Luke)
       
   148     requestor notNil ifTrue:[
       
   149         requestor error:message position:start to:end from:self.
       
   150         ^ self
       
   151     ].
       
   152     Parser::ParseError isHandled ifTrue:[
       
   153         err := Parser::ParseError new.
       
   154         err errorMessage: message startPosition: start endPosition: end.
       
   155         err parameter:self.
       
   156         err lineNumber:line.
       
   157         err raiseRequest.
       
   158         ^ self
       
   159     ].
       
   160 
       
   161     "Created: / 27-02-2012 / 21:10:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   162 !
       
   163 
       
   164 handleException: javaError
       
   165 
       
   166     | errors cause |
       
   167              "Arggghhh...that's Javas' clean OO design, sigh."
       
   168     errors := javaError parameter getErrorCollector getErrors.
       
   169     cause := errors getFirst getCause.
       
   170     self 
       
   171         error:  javaError parameter getMessage
       
   172          line: cause getLine
       
   173          from: cause getStartColumn
       
   174            to: cause getEndColumn.
       
   175 
       
   176     "Created: / 27-02-2012 / 21:09:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   109 ! !
   177 ! !
   110 
   178 
   111 !GroovyCompiler methodsFor:'initialization'!
   179 !GroovyCompiler methodsFor:'initialization'!
   112 
   180 
   113 initialize
   181 initialize