Mercurial Hosting > luan
comparison core/src/luan/impl/LuanParser.java @ 180:5351374efb6d
check for valid variable name in import
git-svn-id: https://luan-java.googlecode.com/svn/trunk@181 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Tue, 24 Jun 2014 04:26:56 +0000 |
parents | bf9c7111a371 |
children | 5d2cb8c1f844 |
comparison
equal
deleted
inserted
replaced
179:bf9c7111a371 | 180:5351374efb6d |
---|---|
365 return parser.failure(null); | 365 return parser.failure(null); |
366 int i = modName.lastIndexOf('/'); | 366 int i = modName.lastIndexOf('/'); |
367 if( i == -1 ) | 367 if( i == -1 ) |
368 i = modName.lastIndexOf('.'); | 368 i = modName.lastIndexOf('.'); |
369 String varName = modName.substring(i+1); | 369 String varName = modName.substring(i+1); |
370 if( !isValidName(varName) ) | |
371 throw parser.exception("invalid variable name '"+varName+"' in import"); | |
370 LuanSource.Element se = se(start); | 372 LuanSource.Element se = se(start); |
371 FnCall require = new FnCall( se, importExpr, new ConstExpr(modName) ); | 373 FnCall require = new FnCall( se, importExpr, new ConstExpr(modName) ); |
372 Settable settable; | 374 Settable settable; |
373 if( interactive ) { | 375 if( interactive ) { |
374 settable = nameVar(se,varName).settable(); | 376 settable = nameVar(se,varName).settable(); |
375 } else { | 377 } else { |
376 addSymbol( varName ); | 378 addSymbol( varName ); |
377 settable = new SetLocalVar(symbolsSize()-1); | 379 settable = new SetLocalVar(symbolsSize()-1); |
378 } | 380 } |
379 return parser.success( new SetStmt( settable, expr(require) ) ); | 381 return parser.success( new SetStmt( settable, expr(require) ) ); |
382 } | |
383 | |
384 private boolean isValidName(String s) { | |
385 if( s.length() == 0 ) | |
386 return false; | |
387 char c = s.charAt(0); | |
388 if( !('a'<=c && c<='z' || 'A'<=c && c<='Z' || c=='_') ) | |
389 return false; | |
390 for( int i=1; i<s.length() ; i++ ) { | |
391 if( !('a'<=c && c<='z' || 'A'<=c && c<='Z' || c=='_' || '0'<=c && c<='9') ) | |
392 return false; | |
393 } | |
394 return true; | |
380 } | 395 } |
381 | 396 |
382 private Stmt BreakStmt() throws ParseException { | 397 private Stmt BreakStmt() throws ParseException { |
383 parser.begin(); | 398 parser.begin(); |
384 if( !Keyword("break",In.NOTHING) ) | 399 if( !Keyword("break",In.NOTHING) ) |