Skip to content

Commit 5e5fd29

Browse files
committed
Merge pull request #93 from prezi/ast
Semantic AST-based code generation
2 parents 28f7637 + 0fceb78 commit 5e5fd29

File tree

201 files changed

+4266
-2210
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

201 files changed

+4266
-2210
lines changed

gradle-spaghetti-plugin/src/main/groovy/com/prezi/spaghetti/gradle/AbstractBundleModuleTask.groovy

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.prezi.spaghetti.gradle
22

3+
import com.prezi.spaghetti.ast.ModuleNode
34
import com.prezi.spaghetti.bundle.ModuleBundle
45
import com.prezi.spaghetti.bundle.ModuleBundleFactory
56
import com.prezi.spaghetti.bundle.ModuleBundleParameters
6-
import com.prezi.spaghetti.definition.ModuleConfiguration
7-
import com.prezi.spaghetti.definition.ModuleDefinition
7+
import com.prezi.spaghetti.config.ModuleConfiguration
88
import org.gradle.api.tasks.Input
99
import org.gradle.api.tasks.InputDirectory
1010
import org.gradle.api.tasks.InputFile
@@ -81,14 +81,14 @@ class AbstractBundleModuleTask extends AbstractDefinitionAwareSpaghettiTask {
8181
}
8282
def config = readConfig(moduleDefinitions)
8383
def module = config.getLocalModules().iterator().next()
84-
def processedJavaScript = createGenerator(config).processModuleJavaScript(module, config, getInputFile().text)
84+
def processedJavaScript = createGenerator(config).processModuleJavaScript(module, getInputFile().text)
8585

8686
createBundle(config, module, processedJavaScript, getSourceMap()?.text, getResourcesDirectory())
8787
}
8888

8989
protected ModuleBundle createBundle(
9090
ModuleConfiguration config,
91-
ModuleDefinition module,
91+
ModuleNode module,
9292
String javaScript,
9393
String sourceMap,
9494
File resourceDir) {
@@ -98,7 +98,7 @@ class AbstractBundleModuleTask extends AbstractDefinitionAwareSpaghettiTask {
9898
getOutputDirectory(),
9999
new ModuleBundleParameters(
100100
name: module.name,
101-
definition: module.definitionSource,
101+
definition: module.source.contents,
102102
version: String.valueOf(project.version),
103103
sourceBaseUrl: getSourceBaseUrl(),
104104
javaScript: javaScript,

gradle-spaghetti-plugin/src/main/groovy/com/prezi/spaghetti/gradle/AbstractPlatformAwareSpaghettiTask.groovy

+1-34
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ package com.prezi.spaghetti.gradle
22

33
import com.prezi.spaghetti.Generator
44
import com.prezi.spaghetti.Platforms
5-
import com.prezi.spaghetti.bundle.ModuleBundle
6-
import com.prezi.spaghetti.definition.ModuleConfiguration
7-
import com.prezi.spaghetti.definition.ModuleConfigurationParser
8-
import com.prezi.spaghetti.definition.ModuleDefinitionSource
5+
import com.prezi.spaghetti.config.ModuleConfiguration
96
import org.gradle.api.tasks.Input
107

118
/**
@@ -18,34 +15,4 @@ class AbstractPlatformAwareSpaghettiTask extends AbstractSpaghettiTask {
1815
protected Generator createGenerator(ModuleConfiguration config) {
1916
return Platforms.createGeneratorForPlatform(getPlatform(), config)
2017
}
21-
22-
ModuleConfiguration readConfig(Iterable<File> files) {
23-
readConfigInternal(files.collect() { file ->
24-
new ModuleDefinitionSource(file.toString(), file.text)
25-
})
26-
}
27-
28-
ModuleConfiguration readConfig() {
29-
readConfigInternal([])
30-
}
31-
32-
private ModuleConfiguration readConfigInternal(Collection<ModuleDefinitionSource> localDefinitions) {
33-
def bundles = lookupBundles()
34-
def directSources = makeModuleSources(bundles.directBundles)
35-
def transitiveSources = makeModuleSources(bundles.transitiveBundles)
36-
def config = ModuleConfigurationParser.parse(
37-
localDefinitions,
38-
directSources,
39-
transitiveSources,
40-
Platforms.getExterns(getPlatform())
41-
)
42-
logger.info("Loaded configuration: ${config}")
43-
return config
44-
}
45-
46-
private static List<ModuleDefinitionSource> makeModuleSources(Set<ModuleBundle> bundles) {
47-
return bundles.collect { ModuleBundle module ->
48-
return new ModuleDefinitionSource("module: " + module.name, module.definition)
49-
}
50-
}
5118
}

gradle-spaghetti-plugin/src/main/groovy/com/prezi/spaghetti/gradle/AbstractSpaghettiTask.groovy

+32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.prezi.spaghetti.gradle
22

3+
import com.prezi.spaghetti.bundle.ModuleBundle
4+
import com.prezi.spaghetti.config.ModuleConfiguration
5+
import com.prezi.spaghetti.config.ModuleConfigurationParser
6+
import com.prezi.spaghetti.definition.ModuleDefinitionSource
37
import org.gradle.api.artifacts.Configuration
48
import org.gradle.api.file.ConfigurableFileCollection
59
import org.gradle.api.internal.ConventionTask
@@ -54,4 +58,32 @@ class AbstractSpaghettiTask extends ConventionTask {
5458
getDependentModules().files + getAdditionalTransitiveDependentModules().files)
5559
return bundles
5660
}
61+
62+
ModuleConfiguration readConfig(Iterable<File> files) {
63+
readConfigInternal(files.collect() { file ->
64+
new ModuleDefinitionSource(file.toString(), file.text)
65+
})
66+
}
67+
68+
ModuleConfiguration readConfig() {
69+
readConfigInternal([])
70+
}
71+
72+
private ModuleConfiguration readConfigInternal(Collection<ModuleDefinitionSource> localDefinitions) {
73+
def bundles = lookupBundles()
74+
def directSources = makeModuleSources(bundles.directBundles)
75+
def transitiveSources = makeModuleSources(bundles.transitiveBundles)
76+
def config = ModuleConfigurationParser.parse(
77+
localDefinitions,
78+
directSources,
79+
transitiveSources)
80+
logger.info("Loaded configuration: ${config}")
81+
return config
82+
}
83+
84+
private static List<ModuleDefinitionSource> makeModuleSources(Set<ModuleBundle> bundles) {
85+
return bundles.collect { ModuleBundle module ->
86+
return new ModuleDefinitionSource("module: " + module.name, module.definition)
87+
}
88+
}
5789
}

gradle-spaghetti-plugin/src/main/groovy/com/prezi/spaghetti/gradle/ObfuscateModule.groovy

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.prezi.spaghetti.gradle
22

33
import com.prezi.spaghetti.Platforms
4+
import com.prezi.spaghetti.ast.ModuleNode
45
import com.prezi.spaghetti.bundle.ModuleBundle
5-
import com.prezi.spaghetti.definition.ModuleConfiguration
6-
import com.prezi.spaghetti.definition.ModuleDefinition
6+
import com.prezi.spaghetti.config.ModuleConfiguration
77
import com.prezi.spaghetti.obfuscation.ModuleObfuscator
88
import com.prezi.spaghetti.obfuscation.ObfuscationParameters
99
import org.gradle.api.tasks.Input
@@ -21,7 +21,7 @@ class ObfuscateModule extends AbstractBundleModuleTask
2121
@Override
2222
protected ModuleBundle createBundle(
2323
ModuleConfiguration config,
24-
ModuleDefinition module,
24+
ModuleNode module,
2525
String javaScript,
2626
String sourceMap,
2727
File resourceDir) {

spaghetti-core/src/main/antlr/Module.g4

+70-58
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
grammar Module;
22

3-
moduleDefinition : (documentation = Doc)? annotations?
4-
'module' (name = qualifiedName)
5-
('as' (alias = Name))?
3+
moduleDefinition : ( documentation = Doc )? annotations?
4+
'module' qualifiedName
5+
( 'as' Name )?
66
moduleElement*
77
;
88

@@ -12,7 +12,7 @@ moduleElement : importDeclaration
1212
| moduleMethodDefinition
1313
;
1414

15-
importDeclaration : 'import' (name = qualifiedName) ('as' (alias = Name))?
15+
importDeclaration : 'import' qualifiedName ( 'as' Name )?
1616
;
1717

1818
typeDefinition : interfaceDefinition
@@ -21,9 +21,9 @@ typeDefinition : interfaceDefinition
2121
| enumDefinition
2222
;
2323

24-
interfaceDefinition : (documentation = Doc)? annotations?
25-
'interface' (name = Name) typeParameters?
26-
( 'extends' superInterfaceDefinition (',' superInterfaceDefinition )* )?
24+
interfaceDefinition : ( documentation = Doc )? annotations?
25+
'interface' Name typeParameters?
26+
( 'extends' superInterfaceDefinition ( ',' superInterfaceDefinition )* )?
2727
'{'
2828
interfaceMethodDefinition*
2929
'}'
@@ -32,116 +32,128 @@ interfaceDefinition : (documentation = Doc)? annotations?
3232
superInterfaceDefinition : qualifiedName typeArguments?
3333
;
3434

35-
externTypeDefinition : (documentation = Doc)? annotations?
36-
'extern' 'interface' (name = qualifiedName)
35+
externTypeDefinition : ( documentation = Doc )? annotations?
36+
'extern' 'interface' qualifiedName
3737
;
3838

39-
typeParameters : '<' (parameters += typeParameter ) ( ',' ( parameters += typeParameter ) )* '>'
39+
typeParameters : '<' Name ( ',' Name )* '>'
4040
;
4141

42-
typeParameter : (name = Name)
43-
;
44-
45-
structDefinition : (documentation = Doc)? annotations?
46-
'struct' (name = Name) '{'
42+
structDefinition : ( documentation = Doc )? annotations?
43+
'struct' Name '{'
4744
propertyDefinition*
4845
'}'
4946
;
5047

51-
constDefinition : (documentation = Doc)? annotations?
52-
'const' ( name = Name ) '{'
48+
constDefinition : ( documentation = Doc )? annotations?
49+
'const' Name '{'
5350
constEntry*
5451
'}'
5552
;
5653

57-
constEntry : (documentation = Doc)? annotations?
54+
constEntry : ( documentation = Doc )? annotations?
5855
constEntryDecl
5956
;
6057

6158
constEntryDecl
62-
: boolType? ( name = Name ) '=' ( boolValue = Boolean )
63-
| intType? ( name = Name ) '=' ( intValue = Integer )
64-
| floatType? ( name = Name ) '=' ( floatValue = Float )
65-
| stringType? ( name = Name ) '=' ( stringValue = String )
59+
: boolType? Name '=' Boolean
60+
| intType? Name '=' Integer
61+
| floatType? Name '=' Float
62+
| stringType? Name '=' String
6663
;
6764

68-
enumDefinition : (documentation = Doc)? annotations?
69-
'enum' (name = Name) '{'
70-
(values += enumValue)*
65+
enumDefinition : ( documentation = Doc )? annotations?
66+
'enum' Name '{'
67+
enumValue*
7168
'}'
7269
;
7370

74-
enumValue : (documentation = Doc)? annotations?
75-
(name = Name)
71+
enumValue : ( documentation = Doc )? annotations?
72+
Name
7673
;
7774

78-
moduleMethodDefinition : (documentation = Doc)? annotations?
75+
moduleMethodDefinition : ( documentation = Doc )? annotations?
7976
(isStatic = 'static')?
8077
methodDefinition
8178
;
8279

83-
interfaceMethodDefinition : (documentation = Doc)? annotations?
80+
interfaceMethodDefinition : ( documentation = Doc )? annotations?
8481
methodDefinition
8582
;
8683

8784
methodDefinition :
8885
typeParameters?
89-
returnTypeChain
90-
(name = Name)
91-
'(' ( parameters = typeNamePairs )? ')'
86+
returnType
87+
Name
88+
'(' methodParameters? ')'
9289
;
9390

94-
propertyDefinition : (documentation = Doc)? annotations?
95-
(property = typeNamePair)
91+
propertyDefinition : ( documentation = Doc )? annotations?
92+
typeNamePair
9693
;
9794

9895
annotations : annotation+
9996
;
10097

101-
annotation : '@' (name = Name) ( '(' annotationParameters? ')' )?
98+
annotation : '@' Name ( '(' annotationParameters? ')' )?
10299
;
103100

104101
annotationParameters : annotationValue
105102
| annotationParameter ( ',' annotationParameter )*
106103
;
107104

108-
annotationParameter : ( name = Name ) '=' annotationValue
105+
annotationParameter : Name '=' annotationValue
106+
;
107+
108+
annotationValue : Null
109+
| Boolean
110+
| Integer
111+
| Float
112+
| String
113+
;
114+
115+
methodParameters : methodParameter ( ',' methodParameter )*
109116
;
110117

111-
annotationValue : ( nullValue = Null ) # annotationNullParameter
112-
| ( boolValue = Boolean ) # annotationBooleanParameter
113-
| ( intValue = Integer ) # annotationIntParameter
114-
| ( floatValue = Float ) # annotationFloatParameter
115-
| ( stringValue = String ) # annotationStringParameter
118+
methodParameter : annotations? typeNamePair
116119
;
117120

118-
typeNamePairs : ( elements += typeNamePair ) ( ',' elements += typeNamePair )*
121+
typeNamePair : complexType Name
119122
;
120123

121-
typeNamePair : annotations? (type = typeChain) (name = Name)
124+
returnType
125+
: voidType
126+
| complexType
122127
;
123128

124-
returnTypeChain : voidType # voidReturnTypeChain
125-
| typeChain # normalReturnTypeChain
129+
complexType
130+
: type
131+
| typeChain
126132
;
127133

128-
typeChain : valueType
129-
| callbackTypeChain
134+
type
135+
: primitiveType ArrayQualifier*
136+
| objectType ArrayQualifier*
130137
;
131138

132-
callbackTypeChain : ( elements += typeChainElement ) ( '->' ( elements += typeChainElement) )+
139+
typeChain
140+
: typeChainElements
141+
| '(' typeChainElements ')' ArrayQualifier+
133142
;
134143

135-
typeChainElement : returnType # simpleTypeChainElement
136-
| '(' typeChain ')' # subTypeChainElement
144+
typeChainElements
145+
: voidType '->' typeChainReturnType
146+
| typeChainElement ( '->' typeChainElement )* '->' typeChainReturnType
137147
;
138148

139-
returnType : voidType
140-
| valueType
149+
typeChainReturnType
150+
: voidType
151+
| typeChainElement
141152
;
142153

143-
valueType : primitiveType ArrayQualifier*
144-
| moduleType ArrayQualifier*
154+
typeChainElement
155+
: type
156+
| '(' typeChain ')'
145157
;
146158

147159
primitiveType : boolType
@@ -169,21 +181,21 @@ anyType : 'any'
169181
voidType : 'void'
170182
;
171183

172-
moduleType : ( name = qualifiedName ) ( arguments = typeArguments )?
184+
objectType : qualifiedName typeArguments?
173185
;
174186

175-
typeArguments : '<' ( arguments += returnType ) ( ',' ( arguments += returnType ) )* '>'
187+
typeArguments : '<' returnType ( ',' returnType )* '>'
176188
;
177189

178-
qualifiedName : ( parts += Name ) ( '.' parts += Name )*
190+
qualifiedName : Name ( '.' Name )*
179191
;
180192

181193
Null : 'null';
182194
Boolean : ( 'true' | 'false' );
183195
Integer : SIGN? INTEGER_NUMBER;
184196
Float : SIGN? NON_INTEGER_NUMBER;
185197
String : '"' STRING_GUTS '"';
186-
Doc : '/**' .*? '*/' '\r'* '\n'?;
198+
Doc : '/**' .*? '*/';
187199
Name : [_a-zA-Z][_a-zA-Z0-9]*;
188200
ArrayQualifier : '[' ']';
189201

0 commit comments

Comments
 (0)