Changeset 440
- Timestamp:
- 10/21/15 02:55:11 (9 years ago)
- Location:
- java
- Files:
-
- 1 added
- 3 deleted
- 12 edited
- 6 copied
Legend:
- Unmodified
- Added
- Removed
-
java/Framclipse/com.framsticks.framclipse.ui/src/com/framsticks/framclipse/ui/labeling/FramScriptLabelProvider.java
r437 r440 112 112 return "class.gif"; 113 113 } 114 115 public String text(EObject obj) { 116 return obj.eClass().getName(); 117 } 114 118 } -
java/Framclipse/com.framsticks.framclipse/src/com/framsticks/framclipse/FramScript.xtext
r437 r440 1 grammar com.framsticks.framclipse.FramScript with org.eclipse.xtext.common.Terminals 1 grammar com.framsticks.framclipse.FramScript with org.eclipse.xtext.common.Terminals hidden(SL_COMMENT, ML_COMMENT, WS) 2 2 3 3 import "http://www.eclipse.org/emf/2002/Ecore" as ecore … … 54 54 'prop:' 55 55 headers += PropertyHeader* 56 'id:' name=ID 56 'id:' name=ID 57 57 headers += PropertyHeader+ 58 58 ; 59 59 60 60 State : 61 'state:' 61 'state:' 62 62 headers += PropertyHeader* 63 'id:' name=ID 63 'id:' name=ID 64 64 headers += PropertyHeader+ 65 65 ; 66 66 67 67 Include : {Include} 68 code=Code? 69 (properties+=Property | 70 states+=State)* 68 ( 69 code=Code? 70 (properties+=Property | 71 states+=State)* 72 ) | {Block} statements+=Statement+ 71 73 ; 72 74 73 75 Header : 74 name= ID value=HEADER_VALUE ;75 76 name=HEADER_ID value=HEADER_VALUE ; 77 76 78 NeuroHeader returns Header : 77 79 {IntHeader} name=('prefinputs:' | 'prefoutput:' | 'preflocation:' | 'vhints:') intValue=IntValue | … … 79 81 {IconHeader} name='icon:~' icon=Icon '~' | 80 82 Header ; 81 82 Icon : 83 84 Icon : 83 85 INT (',' INT)+ ; 84 86 85 87 PropertyHeader returns Header : 86 88 TypeHeader | 87 89 FlagsHeader | 88 90 Header ; 89 91 90 92 ShowHeader returns Header : 91 name='expdef:' value=ID |93 name='expdef:' importURI=ID | 92 94 Header ; 93 95 94 96 FlagsHeader returns Header : 95 97 {IntHeader} name='flags:' intValue=INT ; 96 98 97 99 TypeHeader : 98 name='type:' type=PropertyType ; 99 100 name=TYPE_HEADER type=PropertyType ; 101 102 // This terminal is defined separately for use with FramScriptAuxiliaryLexer. 103 terminal TYPE_HEADER : 'type:'; 104 100 105 PropertyType : 101 name=ID (min=Number (max=Number default=Number?)? enums+=ENUM_LITERAL* )? ; 102 103 terminal ENUM_LITERAL : 104 '~' !('~'|'\r'|'\n')+ ; 105 106 Number : 107 '-'? (INT | DOUBLE) ; 108 106 name=ID (min=Number max=Number default=(Number|HEADER_VALUE)? enums+=ENUM_LITERAL* )? ; 107 108 Number : IntNumber | DoubleNumber ; 109 IntNumber : '-'? INT ; 110 DoubleNumber : '-'? DOUBLE ; 111 109 112 CodeSection returns Code : 110 113 'code:~' Code '~' ; … … 123 126 IncludeDeclaration : 124 127 '@include' importURI=STRING ; 125 128 129 /* 130 * For some reason, the '#include' keyword is not matched. 131 * It is possible it has been overshadowed by SL_COMMENT, 132 * despite the order in which the two have been defined. 133 * This has been worked around in FramScriptAuxiliaryLexer. 134 */ 126 135 PropertyIncludeDeclaration : 127 '#include' importURI=STRING ; 128 136 PROP_INCLUDE importURI=STRING ; 137 138 // keyword separated for use in FramScriptAuxiliaryLexer 139 terminal PROP_INCLUDE : '#include' ; 140 129 141 Block : 130 {Block} '{' statements+=Statement*'}' ;131 132 Statement : 142 '{' statements+=Statement+ '}' ; 143 144 Statement : 133 145 ( VariableDeclarationStatement | 134 146 ExpressionStatement | … … 145 157 WhileStatement | 146 158 ForStatement | 159 ForEachStatement | 147 160 {EmptyStatement} ';' ; 148 161 … … 154 167 155 168 IfStatement : 156 'if' '(' condition=Expression ')' if=Statement ('else' else=Statement)? ; 157 158 ForStatement : 159 'for' '(' init=Assignment? ';' condition=Expression? ';' step=Assignment? ')' body=Statement ; 160 169 'if' '(' condition=Expression ')' if=Statement (=>'else' else=Statement)? ; 170 171 ForStatement : 172 'for' '(' (init=Assignment | init=VariableDeclarationStatement)? ';' condition=Expression? ';' step=Assignment? ')' body=Statement ; 173 174 ForEachStatement : 175 'for' '(' (decl=Iterator|ref=[VariableDeclaration]) 'in' colection=Expression ')' body=Statement ; 176 177 Iterator returns VariableDeclaration : 178 'var' name=ID 179 ; 180 161 181 WhileStatement : 162 182 'while' '(' condition=Expression ')' body=Statement ; … … 182 202 SwitchStatement : 183 203 'switch' condition=ParExpression '{' labels+=SwitchGroup* '}' ; 184 185 SwitchGroup : 204 205 SwitchGroup : 186 206 label=SwitchLabel body=SwitchBlock ; 187 207 … … 206 226 207 227 Expression : 208 UnaryExpression ({Expression.left=current} op=Operator right=Expression)? | 228 UnaryExpression ({Expression.left=current} op=Operator right=Expression)? | 209 229 WildcardExpression ; 210 211 UnaryExpression 230 231 UnaryExpression : 212 232 QualifiedExpression2 ({UnaryExpression.arg=current} op = IncrementOperator)? | 213 233 op = (IncrementOperator | UnaryOperator) arg = QualifiedExpression2 | 234 FunctionLiteral | 214 235 Literal ; 215 236 216 237 QualifiedExpression2 returns QualifiedExpression : 217 238 QualifiedExpression | 218 239 (PropertyAccess|StateAccess) ({QualifiedExpression.parent=current}'.' child=QualifiedExpression )? ; 219 240 220 241 QualifiedExpression : 221 ArrayElementExpression ({QualifiedExpression.parent=current}'.' child=QualifiedExpression )? ; 222 242 MemberAccess ({QualifiedExpression.parent=current}'.' child=QualifiedExpression )? ; 243 244 MemberAccess returns QualifiedExpression: 245 ArrayElementExpression ( {MemberAccess.left=current} '.[' child=Expression ']')? ; 246 223 247 ArrayElementExpression : 224 248 TerminalExpression ({ArrayElementExpression.array=current} ('[' indexes+=Expression ']')+ )? ; 225 249 226 250 TerminalExpression: 251 Cast | 252 TypeOf | 227 253 Invocation | 228 254 Array | 229 Vector | 255 Vector | 256 Dictionary | 230 257 {VariableRef} var=[VariableDeclaration]; 231 258 259 Cast : 260 type=('int'|'float'|'string') '(' expression=Expression ')' ; 261 262 TypeOf : 263 'typeof' '(' expression=Expression ')' ; 264 232 265 Invocation : 233 266 function=[Function] '(' (args+=Expression (',' args+=Expression)*)? ')' ; 234 267 235 268 WildcardExpression : 236 269 value=ID '.*' ; 237 270 238 271 PropertyAccess returns QualifiedExpression : 239 ('Fields.'|'ExpParams.'|'ShowParams.'|'VisParams.') property=[Property];240 272 {PropertyAccess} ('Fields'|'ExpParams'|'ShowParams'|'VisParams') ('.*' | =>('.' property=[Property]) | '.[' (=>property=[Property|STRING] | child=Expression ) ']')? ; 273 241 274 StateAccess returns QualifiedExpression : 242 'ExpState.' state=[State] ; 243 275 {StateAccess} 'ExpState' (".*" | =>('.' state=[State]) | '.[' (=>state=[State|STRING] | child=Expression ) ']')? ; 276 277 FunctionLiteral: 278 'function' function=[Function] ; 279 244 280 Literal: 245 {IntLiteral} value=IntValue | 246 {DoubleLiteral} value=DoubleValue | 247 {StringLiteral} value=STRING | 248 {HexLiteral} value=HEX | 281 {IntLiteral} value=IntValue | 282 {DoubleLiteral} value=DoubleValue | 283 {StringLiteral} value=STRING | 284 {RawStringLiteral} value=ML_STRING | 285 {HexLiteral} value=HEX | 249 286 {NullLiteral} 'null' ; 250 287 251 288 Array : 252 '[' elements+=Expression (',' elements+=Expression)*']' ;289 {Array} '[' (elements+=Expression (',' elements+=Expression)*)? ']' ; 253 290 254 291 Vector : 255 292 '(' elements+=Expression (',' elements+=Expression)* ')' ; 256 293 257 Operator : 258 '+' | '-' | '*'| '/' | 294 Dictionary : 295 {Dictionary} '{' ( keys+=DictKey values+=Expression (',' keys+=DictKey values+=Expression)* )? '}' ; 296 297 DictKey : 298 STRING ':' ; 299 300 Operator : 301 '+' | '-' | '*'| '/' | 259 302 '&'| '|' | '%' | 260 303 '==' | '!=' | '>=' | '<=' | '<' | '>' | 261 304 '&&' | '||' ; 262 305 263 306 UnaryOperator : 264 '!' | '+' | '-' | 'typeof';265 307 '!' | '+' | '-'; 308 266 309 IncrementOperator : 267 310 '++' | '--' ; … … 272 315 DoubleValue hidden() : 273 316 ('+'|'-')? DOUBLE ; 274 275 terminal DOUBLE : 317 318 terminal DOUBLE : 276 319 ('0'..'9')*'.'('0'..'9')+ ('e' INT)? | 277 320 INT 'e' INT ; 278 279 terminal HEX : 321 322 terminal ENUM_LITERAL : 323 '~' !('~'|'\r'|'\n')+ ; 324 325 terminal HEX : 280 326 '0x' (('0'..'9')|('a'..'f')|('A'..'F'))+ ; 281 282 terminal HEADER_VALUE : 283 ':' !('~'|' '|'\t'|'\r'|'\n') !('\n'|'\r')* ('\r'? '\n') | 284 ':~' -> '~'('\r'? '\n') 285 ; 286 287 terminal SL_COMMENT : ('//' | '# ') !('\n'|'\r')* ('\r'? '\n')?; 288 327 328 terminal ML_STRING : 329 '"""' -> '"""' | 330 "'''" -> "'''" 331 ; 332 333 terminal SL_COMMENT : ('//' | '#') -> NL ; 334 335 terminal HEADER_ID : 336 ( 337 'name' | 338 'longname' | 339 'description' | 340 'group' | 341 'help' | 342 'info' | 343 'neurons' 344 ) ':' 345 ; 346 347 /* 348 * This rule should not match anything, 349 * so expect an error when generating the artifacts. 350 * If it is not there, something went wrong. 351 * This rule is here just to make Xtext generate a constant 352 * to be used in a custom lexer (FramScriptAuriliaryLexer), 353 * which takes context into account, 354 * so it is able to recognise HEADER_VALUE 355 * without conflicting with Framscript code syntax. 356 * It should match the following rules, 357 * but only when preceded with HEADER_ID: 358 * '~' NL ('\\~'|!'~')* '~' NL | -> NL, 359 * where NL is defined as: '\r' '\n'? | '\n', 360 * The final NL is not matched, but it is a part of context. 361 * This token is also emitted for the default value in PropertyType. 362 */ 363 terminal HEADER_VALUE : 'name:'; 364 365 terminal fragment NL : '\r' '\n'? | '\n' ; -
java/Framclipse/com.framsticks.framclipse/src/com/framsticks/framclipse/FramScriptRuntimeModule.java
r437 r440 16 16 import com.framsticks.framclipse.script.context.Framscontext; 17 17 import com.framsticks.framclipse.script.model.Framscript; 18 import com.google.inject.Binder; 18 19 import com.thoughtworks.xstream.XStream; 19 20 20 21 /** 21 * Use this class to register components to be used within the IDE.22 * Use this class to register components to be used at runtime / without the Equinox extension registry. 22 23 */ 23 24 public class FramScriptRuntimeModule extends com.framsticks.framclipse.AbstractFramScriptRuntimeModule { … … 56 57 } 57 58 59 @Override 60 public void configureRuntimeLexer(Binder binder) { 61 binder.bind(org.eclipse.xtext.parser.antlr.Lexer.class) 62 .annotatedWith(com.google.inject.name.Names.named(org.eclipse.xtext.parser.antlr.LexerBindings.RUNTIME)) 63 .to(FramScriptAuxiliaryLexer.class); 64 } 65 58 66 } -
java/Framclipse/com.framsticks.framclipse/src/com/framsticks/framclipse/FramScriptValueConverterService.java
r438 r440 18 18 protected String internalToValue(String s, INode node) 19 19 throws ValueConverterException { 20 if (s.startsWith(":~")) { 21 return s.substring(2, s.lastIndexOf('~')).trim(); 22 } else { 23 return s.substring(1).trim(); 20 String r = s; 21 if (s.startsWith("~")) { 22 r = s.substring(1, s.lastIndexOf('~')); 24 23 } 24 return r.trim().replaceAll("\\~", "~"); 25 25 } 26 26 27 27 }; 28 28 } 29 29 30 @ValueConverter(rule = "ML_STRING") 31 public IValueConverter<String> ML_STRING() { 32 33 return new AbstractToStringConverter<String>() { 34 35 @Override 36 protected String internalToValue(String s, INode node) 37 throws ValueConverterException { 38 return s.substring(3, s.length()-3); 39 } 40 41 }; 42 } 30 43 } -
java/Framclipse/com.framsticks.framclipse/src/com/framsticks/framclipse/GenerateFramScript.mwe2
r438 r440 80 80 // Java-based API for validation 81 81 fragment = validation.JavaValidatorFragment auto-inject { 82 //composedCheck = "org.eclipse.xtext.validation.ImportUriValidator"83 //composedCheck = "org.eclipse.xtext.validation.NamesAreUniqueValidator"82 composedCheck = "org.eclipse.xtext.validation.ImportUriValidator" 83 composedCheck = "org.eclipse.xtext.validation.NamesAreUniqueValidator" 84 84 } 85 85 -
java/Framclipse/com.framsticks.framclipse/src/com/framsticks/framclipse/formatting/FramScriptFormatter.java
r437 r440 17 17 18 18 /** 19 * This class contains custom formatting de scription.19 * This class contains custom formatting declarations. 20 20 * 21 * see : http://www.eclipse.org/Xtext/documentation/latest/xtext.html#formatting22 * on how and when to use it 21 * See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#formatting 22 * on how and when to use it. 23 23 * 24 * Also see {@link org.eclipse.xtext.xtext.XtextFormattingTokenSerializer} as an 25 * example 24 * Also see {@link org.eclipse.xtext.xtext.XtextFormattingTokenSerializer} as an example 26 25 */ 27 26 public class FramScriptFormatter extends AbstractDeclarativeFormatter { 28 29 @Override30 protected FramScriptGrammarAccess getGrammarAccess() {31 return (FramScriptGrammarAccess) super.getGrammarAccess();32 }33 34 27 @Override 35 28 protected void configureFormatting(FormattingConfig c) { 36 FramScriptGrammarAccess f = getGrammarAccess();29 FramScriptGrammarAccess f = (FramScriptGrammarAccess) getGrammarAccess(); 37 30 38 31 //Headers … … 68 61 c.setNoSpace().before(f.getFlagsHeaderAccess().getIntValueAssignment_2()); 69 62 70 c.setNoSpace().after(f.getTypeHeaderAccess().getNameT ypeKeyword_0_0());63 c.setNoSpace().after(f.getTypeHeaderAccess().getNameTYPE_HEADERTerminalRuleCall_0_0()); 71 64 c.setLinewrap().after(f.getTypeHeaderAccess().getTypeAssignment_1()); 72 65 … … 94 87 // BlockIndentation 95 88 BlockElements block = f.getBlockAccess(); 96 c.setIndentation(block.getLeftCurlyBracketKeyword_ 1(),97 block.getRightCurlyBracketKeyword_ 3());98 c.setLinewrap().before(block.getLeftCurlyBracketKeyword_ 1());99 c.setLinewrap().after(block.getLeftCurlyBracketKeyword_ 1());100 c.setLinewrap().after(block.getRightCurlyBracketKeyword_ 3());89 c.setIndentation(block.getLeftCurlyBracketKeyword_0(), 90 block.getRightCurlyBracketKeyword_2()); 91 c.setLinewrap().before(block.getLeftCurlyBracketKeyword_0()); 92 c.setLinewrap().after(block.getLeftCurlyBracketKeyword_0()); 93 c.setLinewrap().after(block.getRightCurlyBracketKeyword_2()); 101 94 102 95 // Param … … 104 97 c.setNoLinewrap().around(fullStop); 105 98 c.setNoSpace().around(fullStop); 106 c.setNoSpace().after(f.getPropertyAccessAccess().getAlternatives_0()); 99 c.setNoSpace().after(f.getPropertyAccessAccess().getAlternatives_1()); 100 c.setNoSpace().after(f.getPropertyAccessAccess().getAlternatives_2()); 107 101 108 102 // Invocation … … 117 111 118 112 //Arrays 119 c.setNoSpace().after(f.getArrayAccess().getLeftSquareBracketKeyword_ 0());113 c.setNoSpace().after(f.getArrayAccess().getLeftSquareBracketKeyword_1()); 120 114 c.setNoSpace().before(f.getArrayAccess().getRightSquareBracketKeyword_3()); 121 c.setNoSpace().before(f.getArrayAccess().getCommaKeyword_2_ 0());115 c.setNoSpace().before(f.getArrayAccess().getCommaKeyword_2_1_0()); 122 116 123 117 // Unary expression -
java/Framclipse/com.framsticks.framclipse/src/com/framsticks/framclipse/resource/FramScriptResourceDescription.java
r438 r440 13 13 import com.framsticks.framclipse.framScript.State; 14 14 import com.framsticks.framclipse.framScript.VariableDeclaration; 15 import com.framsticks.framclipse.framScript.VariableDeclarations; 15 16 import com.google.common.base.Predicate; 16 17 import com.google.common.collect.Iterables; … … 19 20 public class FramScriptResourceDescription extends DefaultResourceDescription { 20 21 22 private Resource resource; 23 21 24 public FramScriptResourceDescription(Resource resource, 22 25 IDefaultResourceDescriptionStrategy resourceDescriptionStrategy) { 23 26 super(resource, resourceDescriptionStrategy); 27 this.resource = resource; 24 28 } 25 29 … … 49 53 if (input instanceof VariableDeclaration) { 50 54 VariableDeclaration dec = (VariableDeclaration) input; 51 return dec.eContainer().eContainer() instanceof Code; 55 EObject decs = dec.eContainer(); 56 return decs instanceof VariableDeclarations && 57 (decs.eContainer() instanceof Code || 58 "inc".equals(resource.getURI().fileExtension()) && 59 decs.eContainer().eContainer() == null); 52 60 } 53 61 return false; -
java/Framclipse/com.framsticks.framclipse/src/com/framsticks/framclipse/scoping/FramScriptImportUriResolver.java
r437 r440 8 8 public class FramScriptImportUriResolver extends ImportUriResolver { 9 9 @Override 10 public String apply(EObject from) {10 public String resolve(EObject from) { 11 11 if (from instanceof Header) { 12 12 Header header = (Header) from; 13 13 if (header.getName().equals("expdef:")) { 14 return header.get Value() + ".expdef";14 return header.getImportURI() + ".expdef"; 15 15 } 16 16 } 17 return super. apply(from);17 return super.resolve(from); 18 18 } 19 19 } -
java/Framclipse/com.framsticks.framclipse/src/com/framsticks/framclipse/scoping/FramScriptScopeProvider.java
r438 r440 20 20 import org.eclipse.emf.ecore.EStructuralFeature; 21 21 import org.eclipse.emf.ecore.resource.Resource; 22 import org.eclipse.emf.ecore.resource.ResourceSet; 22 23 import org.eclipse.emf.ecore.resource.impl.ResourceImpl; 23 24 import org.eclipse.xtext.resource.EObjectDescription; 24 25 import org.eclipse.xtext.resource.IEObjectDescription; 26 import org.eclipse.xtext.resource.IResourceDescription; 27 import org.eclipse.xtext.scoping.IGlobalScopeProvider; 25 28 import org.eclipse.xtext.scoping.IScope; 26 29 import org.eclipse.xtext.scoping.Scopes; … … 30 33 import com.framsticks.framclipse.framScript.Block; 31 34 import com.framsticks.framclipse.framScript.Code; 35 import com.framsticks.framclipse.framScript.Expdef; 36 import com.framsticks.framclipse.framScript.ForEachStatement; 37 import com.framsticks.framclipse.framScript.ForStatement; 32 38 import com.framsticks.framclipse.framScript.FramScriptFactory; 33 39 import com.framsticks.framclipse.framScript.Function; 34 40 import com.framsticks.framclipse.framScript.GotoStatment; 41 import com.framsticks.framclipse.framScript.Header; 42 import com.framsticks.framclipse.framScript.IncludeDeclaration; 35 43 import com.framsticks.framclipse.framScript.Invocation; 36 44 import com.framsticks.framclipse.framScript.LabeledStatement; 37 45 import com.framsticks.framclipse.framScript.Model; 46 import com.framsticks.framclipse.framScript.PropertyIncludeDeclaration; 38 47 import com.framsticks.framclipse.framScript.ProposableFunctionImpl; 39 48 import com.framsticks.framclipse.framScript.ProposableVariableDeclarationImpl; 40 49 import com.framsticks.framclipse.framScript.QualifiedExpression; 50 import com.framsticks.framclipse.framScript.Show; 41 51 import com.framsticks.framclipse.framScript.Statement; 42 52 import com.framsticks.framclipse.framScript.VariableDeclaration; 43 53 import com.framsticks.framclipse.framScript.VariableDeclarations; 44 54 import com.framsticks.framclipse.framScript.VariableRef; 55 import com.framsticks.framclipse.resource.FramScriptResourceDescriptionManager; 45 56 import com.framsticks.framclipse.script.ConstantProvider; 46 57 import com.framsticks.framclipse.script.ExpressionTraverser; … … 54 65 * This class contains custom scoping description. 55 66 * 56 * see : http://www.eclipse.org/Xtext/documentation/latest/xtext.html#scoping 57 * on how and when to use it 58 * 67 * See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#scoping 68 * on how and when to use it. 59 69 */ 60 70 public class FramScriptScopeProvider extends AbstractDeclarativeScopeProvider { 61 71 62 72 private final ConstantProvider provider; 63 73 64 74 private final ExpressionTraverser traverser; 65 75 76 @Inject 77 private IGlobalScopeProvider globalScopeProvider; 78 79 @Inject 80 private FramScriptResourceDescriptionManager descriptionManager; 81 66 82 private Resource resource; 67 83 68 84 private Map<String, IScope> typesScopes = Maps.newHashMap(); 69 85 private Map<Type, IScope> functionScopes = Maps.newHashMap(); 70 86 private Map<Type, IScope> fieldScopes = Maps.newHashMap(); 71 87 88 private FramScriptImportUriResolver resolver = new FramScriptImportUriResolver(); 89 72 90 @Inject 73 91 public FramScriptScopeProvider(ConstantProvider provider, ExpressionTraverser traverser) { … … 77 95 initTypes(); 78 96 } 79 97 98 @Override 99 public IScope getScope(EObject context, EReference reference) { 100 if (reference.getEContainingClass().getName().equals("VariableRef") && 101 reference.getName().equals("var")) { 102 if (context instanceof VariableRef) { 103 QualifiedExpression parent = getParent((VariableRef) context); 104 if (parent != null) { 105 return getScope(parent, reference); 106 } 107 EObject object = context; 108 while (object != null && !(object.eContainer() instanceof Block || 109 object.eContainer() instanceof ForStatement || 110 object.eContainer() instanceof ForEachStatement)) { 111 object = object.eContainer(); 112 } 113 Statement statement = (Statement) object; 114 EObject blockOrFor = object.eContainer(); 115 116 if (blockOrFor instanceof Block) { 117 Block block = (Block) blockOrFor; 118 return Scopes.scopeFor(precedingDeclarations(block, statement), 119 nextValidScope(block, reference)); 120 } else 121 return getScope(blockOrFor, reference); 122 } else if (context.eContainer() instanceof Block) { 123 Block block = (Block) context.eContainer(); 124 List<VariableDeclaration> vars = precedingDeclarations(block, (Statement)context); 125 IScope outerScope = Scopes.scopeFor(vars, nextValidScope(block, reference)); 126 if (context instanceof Block) { 127 return outerScope; 128 } 129 return new SimpleScope(outerScope, 130 super.getScope(context, reference).getAllElements()); 131 } 132 } 133 return super.getScope(context, reference); 134 } 135 136 private IScope nextValidScope(EObject context, EReference ref) { 137 if (ref.getEContainingClass().getName().equals("VariableRef") && 138 ref.getName().equals("var")) { 139 if (context instanceof Block) { 140 EObject parent = context.eContainer(); 141 if (parent == null) { // may happen in .inc files. 142 return scope_VariableRef_var((Model)context, ref); 143 } 144 if (!(parent instanceof Block)) { 145 return getScope(parent, ref); 146 } 147 } 148 } 149 return getScope(context, ref); 150 } 151 152 private List<VariableDeclaration> precedingDeclarations(Block block, Statement statement){ 153 List<VariableDeclaration> vars = Lists.newArrayList(); 154 for (Statement stmt : block.getStatements()) { 155 // Everything after the statement is out of scope. 156 if (stmt == statement) { 157 break; 158 } else if (stmt instanceof VariableDeclarations) { 159 vars.addAll(((VariableDeclarations) stmt).getVars()); 160 } 161 } 162 return vars; 163 } 164 80 165 protected void initTypes() { 81 166 List<Function> allFunctions = Lists.newArrayList(); … … 116 201 scope = scope_Invocation_function(getFunction(inv), ref); 117 202 } 118 scope = filterByArgumentsCount(scope, inv.getArgs().size()); 119 return scope; 203 Show show = getShow(inv); 204 IScope showScope = show == null ? IScope.NULLSCOPE : 205 new SimpleScope(filterByArgumentsCount(getScope(show, ref), inv.getArgs().size())); 206 return new SimpleScope(showScope, filterByArgumentsCount(scope, inv.getArgs().size())); 120 207 } 121 208 … … 132 219 } 133 220 134 private IScope filterByArgumentsCount(IScope scope, int size) { 221 protected IScope scope_Invocation_function(Show show, EReference ref) { 222 return addAliases(new SimpleScope(showExpdefIncludes(show, ref))); 223 } 224 225 /* 226 * Make sure scope contains only function descriptions. 227 */ 228 private List<IEObjectDescription> filterByArgumentsCount(IScope scope, int size) { 135 229 List<IEObjectDescription> filtered = Lists.newArrayList(); 136 230 for (IEObjectDescription desc : scope.getAllElements()) { … … 140 234 } 141 235 } 142 return new SimpleScope(filtered);143 } 144 236 return filtered; 237 } 238 145 239 private IScope addAliases(IScope scope) { 146 240 Iterable<IEObjectDescription> contents = scope.getAllElements(); … … 155 249 } 156 250 157 protected IScope scope_VariableRef_var(VariableRef var, EReference ref) {158 QualifiedExpression parent = getParent(var);159 if (parent != null) {160 return scope_VariableRef_var(parent, ref);161 } else {162 return scope_VariableRef_var(getBlock(var), ref);163 }164 }165 166 251 protected IScope scope_VariableRef_var(QualifiedExpression var, EReference ref) { 167 252 Type type = traverser.getLastType(var); 168 253 IScope scope = fieldScopes.get(type); 169 return scope != null ? scope : IScope.NULLSCOPE; 170 } 171 254 return new SimpleScope(nextValidScope(var.eContainer(), ref), 255 scope != null ? scope.getAllElements() : new ArrayList<IEObjectDescription>()); 256 } 257 172 258 protected IScope scope_VariableRef_var(Block block, EReference ref) { 259 // Scope for Blocks is constructed earlier in getScope, so we stop here. 260 return IScope.NULLSCOPE; 261 } 262 263 protected IScope scope_VariableRef_var(ForStatement fs, EReference ref) { 173 264 List<VariableDeclaration> vars = Lists.newArrayList(); 174 for (Statement stmt : block.getStatements()) { 175 if (stmt instanceof VariableDeclarations) { 176 vars.addAll(((VariableDeclarations) stmt).getVars()); 177 } 178 } 179 return Scopes.scopeFor(vars, getScope(block.eContainer(), ref)); 180 } 181 265 Statement init = fs.getInit(); 266 if (init instanceof VariableDeclarations) { 267 vars.addAll(((VariableDeclarations)init).getVars()); 268 } 269 return Scopes.scopeFor(vars, getScope(fs.eContainer(), ref)); 270 } 271 272 protected IScope scope_VariableRef_var(ForEachStatement fes, EReference ref) { 273 List<VariableDeclaration> vars = Lists.newArrayList(); 274 if (fes.getDecl() == null) 275 return getScope(fes.eContainer(), ref); 276 else { 277 vars.add(fes.getDecl()); 278 return Scopes.scopeFor(vars, getScope(fes.eContainer(), ref)); 279 } 280 } 281 282 /* 283 * Blocks are handled specially in getScope, 284 * and scope_VariableRef_var(Block, EReference) returns IScope.NULLSCOPE, 285 * which breaks the chain of scopes, 286 * so we have to ensure each unspecified scope_VariableRef_var call 287 * is redirected to getScope in this class, instead of super.getScope. 288 */ 289 protected IScope scope_VariableRef_var(EObject ctx, EReference ref) { 290 EObject parent = ctx.eContainer(); 291 if (parent == null) 292 return IScope.NULLSCOPE; 293 else 294 return getScope(parent, ref); 295 } 296 182 297 protected IScope scope_VariableRef_var(Function f, EReference ref) { 183 298 List<VariableDeclaration> vars = Lists.newArrayList(); … … 185 300 return Scopes.scopeFor(vars, getScope(f.eContainer(), ref)); 186 301 } 187 302 188 303 protected IScope scope_VariableRef_var(Code code, EReference ref) { 189 304 List<VariableDeclaration> vars = Lists.newArrayList(); … … 193 308 return Scopes.scopeFor(vars, getScope(code.eContainer(), ref)); 194 309 } 195 310 311 private List<IEObjectDescription> showExpdefIncludes(Show show, EReference ref) { 312 ArrayList<IEObjectDescription> result = Lists.newArrayList(); 313 // Get expdef header 314 Header expdefHeader= null; 315 for (Header h : show.getHeaders()) { 316 if ("expdef:".equals(h.getName())) { 317 expdefHeader = h; 318 break; 319 } 320 } 321 if (expdefHeader == null) 322 return result; 323 // Find resource by name 324 ResourceSet resourceSet = show.eResource().getResourceSet(); 325 String expdefFile = resolver.resolve(expdefHeader); 326 Resource expdefResource = findResourceByName(resourceSet, expdefFile); 327 if (expdefResource == null) 328 return result; 329 // Mine include directives 330 ArrayList<String> included = Lists.newArrayList(); 331 Expdef expdef = (Expdef) expdefResource.getContents().get(0); 332 for (PropertyIncludeDeclaration pid : expdef.getPropertyImports()) 333 included.add(pid.getImportURI()); 334 for (IncludeDeclaration id : expdef.getCode().getIncludes()) 335 included.add(id.getImportURI()); 336 // Get globals 337 for (String filename : included) { 338 Resource includedResource = findResourceByName(resourceSet, filename); 339 if (includedResource == null) { 340 String showURI = show.eResource().getURI().toString(); 341 URI includeURI = URI.createURI(showURI.subSequence( 342 0, showURI.lastIndexOf("/")+1) + filename); 343 includedResource = resourceSet.getResource( 344 includeURI, true); 345 if (includedResource == null) 346 continue; 347 } 348 IResourceDescription resourceDescription = 349 descriptionManager.getResourceDescription(includedResource); 350 result.addAll(Lists.newArrayList(resourceDescription.getExportedObjectsByType(ref.getEReferenceType()))); 351 } 352 return result; 353 } 354 355 private Resource findResourceByName(ResourceSet resourceSet, 356 String filename) { 357 for (Resource res : resourceSet.getResources()) { 358 if (res.getURI().toString().endsWith("/" + filename)) { 359 return res; 360 } 361 } 362 return null; 363 } 364 196 365 protected IScope scope_VariableRef_var(Model model, EReference ref) { 197 // IScope outerScope = delegateGetScope(model, ref).getOuterScope(); 198 IScope types = typesScopes.get(getExtension(model)); 199 return new SimpleScope(types.getAllElements()); 366 IScope outerScope = globalScopeProvider.getScope(model.eResource(), ref, null); 367 if (model instanceof Show) { 368 outerScope = new SimpleScope(outerScope, showExpdefIncludes((Show)model, ref)); 369 } 370 String ext = getExtension(model); 371 if ("inc".equals(ext)) // There is no description for .inc in XML, 372 ext = "script"; // so we pretend it is a .script. 373 IScope types = typesScopes.get(ext); 374 return new SimpleScope(outerScope, types.getAllElements()); 200 375 } 201 376 … … 211 386 return Scopes.scopeFor(labels); 212 387 } 213 388 389 protected IScope scope_Property(Show show, EReference ref) { 390 return new SimpleScope(delegateGetScope(show, ref), showExpdefIncludes(show, ref)); 391 } 392 393 protected IScope scope_State(Show show, EReference ref) { 394 return new SimpleScope(delegateGetScope(show, ref), showExpdefIncludes(show, ref)); 395 } 396 214 397 private Function createFunction(Element element) { 215 398 Function function = new ProposableFunctionImpl(element); … … 233 416 return global; 234 417 } 235 418 236 419 private VariableDeclaration createVariableDeclaration(String name) { 237 420 VariableDeclaration var = FramScriptFactory.eINSTANCE.createVariableDeclaration(); … … 240 423 return var; 241 424 } 242 425 243 426 private QualifiedExpression getParent(QualifiedExpression expr) { 244 427 EStructuralFeature feature = expr.eContainingFeature(); 245 428 if (feature.equals(QUALIFIED_EXPRESSION__CHILD)) { 246 429 return (QualifiedExpression) expr.eContainer(); 247 } else if (feature.equals(QUALIFIED_EXPRESSION__PARENT) 430 } else if (feature.equals(QUALIFIED_EXPRESSION__PARENT) 248 431 || feature.equals(ARRAY_ELEMENT_EXPRESSION__ARRAY)) { 249 432 return getParent((QualifiedExpression) expr.eContainer()); … … 252 435 } 253 436 } 254 255 private Block getBlock(EObject object) { 256 while (object != null && !(object instanceof Block)) { 257 object = object.eContainer(); 258 } 259 return (Block) object; 260 } 261 437 262 438 private Function getFunction(EObject object) { 263 439 while (object != null && !(object instanceof Function)) { … … 266 442 return (Function) object; 267 443 } 268 444 445 private Show getShow(EObject object) { 446 while (object != null && !(object instanceof Show)) { 447 object = object.eContainer(); 448 } 449 return (Show) object; 450 } 451 269 452 private String getExtension(EObject object) { 270 453 return object.eResource().getURI().fileExtension(); 271 454 } 272 455 273 456 } -
java/Framclipse/com.framsticks.framclipse/src/com/framsticks/framclipse/script/XMLConstantProvider.java
r438 r440 85 85 for (File file : framscontext.getFiles()) { 86 86 String ext = file.getPattern(); 87 ext = ext.substring(ext. indexOf('.') + 1, ext.length());87 ext = ext.substring(ext.lastIndexOf('.') + 1, ext.length()); 88 88 if (file.getCode() != null) { 89 89 for (Context context : file.getCode().getContexts()) { -
java/Framclipse/com.framsticks.framclipse/src/com/framsticks/framclipse/script/model/Element.java
r437 r440 21 21 @XStreamAlias("default") 22 22 @XStreamAsAttribute 23 private Doubledef;23 private String def; 24 24 25 25 private String description; … … 47 47 } 48 48 49 public DoublegetDef() {49 public String getDef() { 50 50 return def; 51 51 } -
java/Framclipse/com.framsticks.framclipse/src/com/framsticks/framclipse/validation/FramScriptJavaValidator.java
r437 r440 1 /* 2 * generated by Xtext 3 */ 1 4 package com.framsticks.framclipse.validation; 2 5 6 /** 7 * This class contains custom validation rules. 8 * 9 * See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#validation 10 */ 11 public class FramScriptJavaValidator extends com.framsticks.framclipse.validation.AbstractFramScriptJavaValidator { 3 12 4 public class FramScriptJavaValidator extends AbstractFramScriptJavaValidator { 5 13 // @Check 14 // public void checkGreetingStartsWithCapital(Greeting greeting) { 15 // if (!Character.isUpperCase(greeting.getName().charAt(0))) { 16 // warning("Name should start with a capital", MyDslPackage.Literals.GREETING__NAME); 17 // } 18 // } 6 19 }
Note: See TracChangeset
for help on using the changeset viewer.