changeset 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
files core/src/luan/impl/LuanParser.java
diffstat 1 files changed, 15 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/impl/LuanParser.java	Tue Jun 24 00:05:53 2014 +0000
+++ b/core/src/luan/impl/LuanParser.java	Tue Jun 24 04:26:56 2014 +0000
@@ -367,6 +367,8 @@
 		if( i == -1 )
 			i = modName.lastIndexOf('.');
 		String varName = modName.substring(i+1);
+		if( !isValidName(varName) )
+			throw parser.exception("invalid variable name '"+varName+"' in import");
 		LuanSource.Element se = se(start);
 		FnCall require = new FnCall( se, importExpr, new ConstExpr(modName) );
 		Settable settable;
@@ -379,6 +381,19 @@
 		return parser.success( new SetStmt( settable, expr(require) ) );
 	}
 
+	private boolean isValidName(String s) {
+		if( s.length() == 0 )
+			return false;
+		char c = s.charAt(0);
+		if( !('a'<=c && c<='z' || 'A'<=c && c<='Z' || c=='_') )
+			return false;
+		for( int i=1; i<s.length() ; i++ ) {
+			if( !('a'<=c && c<='z' || 'A'<=c && c<='Z' || c=='_' || '0'<=c && c<='9') )
+				return false;
+		}
+		return true;
+	}
+
 	private Stmt BreakStmt() throws ParseException {
 		parser.begin();
 		if( !Keyword("break",In.NOTHING) )