Skip to content

Commit

Permalink
Type-checking script config and DSLD
Browse files Browse the repository at this point in the history
for #1402
  • Loading branch information
eric-milles committed Oct 4, 2022
1 parent 85300d0 commit 4118605
Show file tree
Hide file tree
Showing 10 changed files with 369 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,15 @@ public void testStaticTypeCheckingDSL1() throws Exception {
IPath projPath = createGenericProject();
//@formatter:off
env.addGroovyClass(projPath.append("src"), "RobotMove",
"import org.codehaus.groovy.ast.expr.VariableExpression\n" +
"unresolvedVariable { VariableExpression var ->\n" +
" if ('robot' == var.name) {\n" +
" def robotClass = context.source.AST.classes.find { it.name == 'Robot' }\n" +
" storeType(var, robotClass)\n" +
"unresolvedVariable { VariableExpression vexp ->\n" +
" if ('robot' == vexp.name) {\n" +
" def robotClass = lookupClassNodeFor('Robot')\n" +
" storeType(vexp, robotClass)\n" +
" handled = true\n" +
" }\n" +
"}\n");
env.addGroovyClass(projPath.append("src"), "Robot",
"@groovy.transform.TypeChecked(extensions = 'RobotMove.groovy')\n" +
"@groovy.transform.TypeChecked(extensions='RobotMove.groovy')\n" +
"void operate() {\n" +
" robot.move \"left\"\n" +
"}\n");
Expand All @@ -92,21 +91,19 @@ public void testStaticTypeCheckingDSL2() throws Exception {
IPath projPath = createGenericProject();
//@formatter:off
env.addGroovyClass(projPath.append("src"), "RobotMove",
"import org.codehaus.groovy.ast.expr.VariableExpression\n" +
"unresolvedVariable { VariableExpression var ->\n" +
" if ('robot' == var.name) {\n" +
" def robotClass = context.source.AST.classes.find { it.name == 'Robot' }\n" +
" storeType(var, robotClass)\n" +
"unresolvedVariable { VariableExpression vexp ->\n" +
" if ('robot' == vexp.name) {\n" +
" def robotClass = lookupClassNodeFor('Robot')\n" +
" storeType(vexp, robotClass)\n" +
" handled = true\n" +
" }\n" +
"}\n");
env.addGroovyClass(projPath.append("src"), "RobotScript",
"import groovy.transform.TypeChecked\n" +
"class Robot {\n" +
" void move(String dist) { println \"Moved $dist\" }\n" +
"}\n" +
"robot = new Robot()\n" +
"@TypeChecked(extensions = 'RobotMove.groovy')\n" +
"@groovy.transform.TypeChecked(extensions='RobotMove.groovy')\n" +
"void operate() {\n" +
" robot.move \"left\"\n" +
"}\n");
Expand All @@ -127,7 +124,7 @@ public void testStaticTypeCheckingDSL2() throws Exception {
env.addGroovyClass(projPath.append("src"), "TypeChecker",
"onMethodSelection { expr, node ->\n" +
" if (node.name == 'setS') {\n" +
" def closure = context.enclosingClosure.closureExpression\n" +
" def closure = enclosingClosure.closureExpression\n" +
" def setting = closure.code.statements[0].expression\n" +
" setting.putNodeMetaData('notified', true)\n" +
" }\n" +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2009-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dsld

import org.codehaus.groovy.ast.expr.*
import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.ast.MethodNode
import org.codehaus.groovy.transform.stc.AbstractTypeCheckingExtension
import org.codehaus.groovy.transform.stc.GroovyTypeCheckingExtensionSupport
import org.codehaus.groovy.transform.stc.GroovyTypeCheckingExtensionSupport.TypeCheckingDSL

assertVersion(groovyEclipse: '4.8.0')

contribute(isScript() & currentType(subType(TypeCheckingDSL))) {
provider = "{@link ${GroovyTypeCheckingExtensionSupport.name}.TypeCheckingDSL TypeCheckingDSL}"

delegatesTo type: AbstractTypeCheckingExtension // skip over GroovyTypeCheckingExtensionSupport

method name: 'setDebug', type: void, params: [debug: boolean], declaringType: GroovyTypeCheckingExtensionSupport

for (type in ['AnnotationConstant', 'ArgumentList', 'Array', 'Attribute',
'Binary', 'BitwiseNegation', 'Boolean',
'Cast', 'Class', 'Closure', 'Constant', 'ConstructorCall',
'Declaration',
'ElvisOperator', 'Empty',
'Field',
'GString',
'List',
'Map', 'MapEntry', 'MethodCall', 'MethodPointer',
'NamedArgumentList', 'Not',
'Postfix', 'Prefix', 'Property',
'Range',
'Spread', 'SpreadMap', 'StaticMethodCall',
'Ternary', 'Tuple',
'UnaryMinus', 'UnaryPlus',
'Variable'
]) method name: "is${type}Expression", type: boolean, params: [node: Object], declaringType: TypeCheckingDSL

// event callback registration -----------------------------------------------------------------------------

method name: 'setup', type: void, params: [handler: Closure], noParens: true, declaringType: TypeCheckingDSL

method name: 'finish', type: void, params: [handler: Closure], noParens: true, declaringType: TypeCheckingDSL

method name: 'afterMethodCall', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${MethodCall.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'afterVisitClass', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${ClassNode.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'afterVisitMethod', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${MethodNode.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'ambiguousMethods', type: void, params: [handler: "@ClosureParams(value=FromString, options=['java.util.List<${MethodNode.name}>,${Expression.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'beforeMethodCall', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${MethodCall.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'beforeVisitClass', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${ClassNode.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'beforeVisitMethod', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${MethodNode.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'incompatibleAssignment', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${ClassNode.name},${ClassNode.name},${Expression.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'incompatibleReturnType', type: void, params: [handler: "@ClosureParams(value=FromString, options=['org.codehaus.groovy.ast.stmt.ReturnStatement,${ClassNode.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'methodNotFound', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${ClassNode.name},java.lang.String,${ArgumentListExpression.name},${ClassNode.name}[],${MethodCall.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'onMethodSelection', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${Expression.name},${MethodNode.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'unresolvedAttribute', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${AttributeExpression.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'unresolvedProperty', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${PropertyExpression.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'unresolvedVariable', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${VariableExpression.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2009-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dsld

import org.codehaus.groovy.ast.expr.*
import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.ast.MethodNode
import org.codehaus.groovy.transform.stc.AbstractTypeCheckingExtension
import org.codehaus.groovy.transform.stc.GroovyTypeCheckingExtensionSupport
import org.codehaus.groovy.transform.stc.GroovyTypeCheckingExtensionSupport.TypeCheckingDSL

assertVersion(groovyEclipse: '4.8.0')

contribute(isScript() & currentType(subType(TypeCheckingDSL))) {
provider = "{@link ${GroovyTypeCheckingExtensionSupport.name}.TypeCheckingDSL TypeCheckingDSL}"

delegatesTo type: AbstractTypeCheckingExtension // skip over GroovyTypeCheckingExtensionSupport

method name: 'setDebug', type: void, params: [debug: boolean], declaringType: GroovyTypeCheckingExtensionSupport

for (type in ['AnnotationConstant', 'ArgumentList', 'Array', 'Attribute',
'Binary', 'BitwiseNegation', 'Boolean',
'Cast', 'Class', 'Closure', 'Constant', 'ConstructorCall',
'Declaration',
'ElvisOperator', 'Empty',
'Field',
'GString',
'Lambda', 'List',
'Map', 'MapEntry', 'MethodCall', 'MethodPointer', 'MethodReference',
'NamedArgumentList', 'Not',
'Postfix', 'Prefix', 'Property',
'Range',
'Spread', 'SpreadMap', 'StaticMethodCall',
'Ternary', 'Tuple',
'UnaryMinus', 'UnaryPlus',
'Variable'
]) method name: "is${type}Expression", type: boolean, params: [node: Object], declaringType: TypeCheckingDSL

// event callback registration -----------------------------------------------------------------------------

method name: 'setup', type: void, params: [handler: Closure], noParens: true, declaringType: TypeCheckingDSL

method name: 'finish', type: void, params: [handler: Closure], noParens: true, declaringType: TypeCheckingDSL

method name: 'afterMethodCall', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${MethodCall.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'afterVisitClass', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${ClassNode.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'afterVisitMethod', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${MethodNode.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'ambiguousMethods', type: void, params: [handler: "@ClosureParams(value=FromString, options=['java.util.List<${MethodNode.name}>,${Expression.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'beforeMethodCall', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${MethodCall.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'beforeVisitClass', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${ClassNode.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'beforeVisitMethod', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${MethodNode.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'incompatibleAssignment', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${ClassNode.name},${ClassNode.name},${Expression.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'incompatibleReturnType', type: void, params: [handler: "@ClosureParams(value=FromString, options=['org.codehaus.groovy.ast.stmt.ReturnStatement,${ClassNode.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'methodNotFound', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${ClassNode.name},java.lang.String,${ArgumentListExpression.name},${ClassNode.name}[],${MethodCall.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'onMethodSelection', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${Expression.name},${MethodNode.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'unresolvedAttribute', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${AttributeExpression.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'unresolvedProperty', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${PropertyExpression.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL

method name: 'unresolvedVariable', type: void, params: [handler: "@ClosureParams(value=FromString, options=['${VariableExpression.name}']) @DelegatesTo(${AbstractTypeCheckingExtension.name}) Closure"], noParens: true, declaringType: TypeCheckingDSL
}
Loading

0 comments on commit 4118605

Please sign in to comment.