Jelajahi Sumber

disallow leading zeroes for int and float literals (closes #3295)

Simon Krajewski 11 tahun lalu
induk
melakukan
caaa4bc48b
4 mengubah file dengan 11 tambahan dan 10 penghapusan
  1. 7 6
      lexer.mll
  2. 1 1
      tests/unit/TestMisc.hx
  3. 2 2
      tests/unit/TestSpod.hx
  4. 1 1
      tests/unit/unitstd/DateTools.unit.hx

+ 7 - 6
lexer.mll

@@ -220,6 +220,7 @@ let invalid_char lexbuf =
 
 let ident = ('_'* ['a'-'z'] ['_' 'a'-'z' 'A'-'Z' '0'-'9']* | '_'+ | '_'+ ['0'-'9'] ['_' 'a'-'z' 'A'-'Z' '0'-'9']* )
 let idtype = '_'* ['A'-'Z'] ['_' 'a'-'z' 'A'-'Z' '0'-'9']*
+let integer = ['1'-'9'] ['0'-'9']* | '0'
 
 rule skip_header = parse
 	| "\239\187\191" { skip_header lexbuf }
@@ -232,12 +233,12 @@ and token = parse
 	| "\r\n" { newline lexbuf; token lexbuf }
 	| '\n' | '\r' { newline lexbuf; token lexbuf }
 	| "0x" ['0'-'9' 'a'-'f' 'A'-'F']+ { mk lexbuf (Const (Int (lexeme lexbuf))) }
-	| ['0'-'9']+ { mk lexbuf (Const (Int (lexeme lexbuf))) }
-	| ['0'-'9']+ '.' ['0'-'9']+ { mk lexbuf (Const (Float (lexeme lexbuf))) }
+	| integer { mk lexbuf (Const (Int (lexeme lexbuf))) }
+	| integer '.' ['0'-'9']+ { mk lexbuf (Const (Float (lexeme lexbuf))) }
 	| '.' ['0'-'9']+ { mk lexbuf (Const (Float (lexeme lexbuf))) }
-	| ['0'-'9']+ ['e' 'E'] ['+' '-']? ['0'-'9']+ { mk lexbuf (Const (Float (lexeme lexbuf))) }
-	| ['0'-'9']+ '.' ['0'-'9']* ['e' 'E'] ['+' '-']? ['0'-'9']+ { mk lexbuf (Const (Float (lexeme lexbuf))) }
-	| ['0'-'9']+ "..." {
+	| integer ['e' 'E'] ['+' '-']? ['0'-'9']+ { mk lexbuf (Const (Float (lexeme lexbuf))) }
+	| integer '.' ['0'-'9']* ['e' 'E'] ['+' '-']? ['0'-'9']+ { mk lexbuf (Const (Float (lexeme lexbuf))) }
+	| integer "..." {
 			let s = lexeme lexbuf in
 			mk lexbuf (IntInterval (String.sub s 0 (String.length s - 3)))
 		}
@@ -367,7 +368,7 @@ and string2 = parse
 		string2 lexbuf;
 	}
 	| [^'\'' '\\' '\r' '\n' '$']+ { store lexbuf; string2 lexbuf }
-	
+
 and code_string = parse
 	| eof { raise Exit }
 	| '\n' | '\r' | "\r\n" { newline lexbuf; store lexbuf; code_string lexbuf }

+ 1 - 1
tests/unit/TestMisc.hx

@@ -115,7 +115,7 @@ class TestMisc extends Test {
 	}
 
 	function testDate() {
-		var d = new Date(2012, 07, 17, 01, 02, 03);
+		var d = new Date(2012, 7, 17, 1, 2, 3);
 		eq( d.getDay(), 5 );
 
 		eq( d.getDate(), 17 );

+ 2 - 2
tests/unit/TestSpod.hx

@@ -37,7 +37,7 @@ class TestSpod extends Test
 		scls.double = 2.0;
 		scls.boolean = true;
 		scls.string = "some string";
-		scls.date = new Date(2012, 07, 30, 0, 0, 0);
+		scls.date = new Date(2012, 7, 30, 0, 0, 0);
 
 		var bytes = Bytes.ofString("\x01\n\r\x02");
 		scls.binary = bytes;
@@ -88,7 +88,7 @@ class TestSpod extends Test
 		eq(cls1.string, "some string",pos());
 		t(cls1.date != null,pos());
 		t(Std.is(cls1.date, Date),pos());
-		eq(cls1.date.getTime(), new Date(2012, 07, 30, 0, 0, 0).getTime(),pos());
+		eq(cls1.date.getTime(), new Date(2012, 7, 30, 0, 0, 0).getTime(),pos());
 
 		t(Std.is(cls1.binary, Bytes),pos());
 		eq(cls1.binary.compare(Bytes.ofString("\x01\n\r\x02")), 0,pos());

+ 1 - 1
tests/unit/unitstd/DateTools.unit.hx

@@ -1,5 +1,5 @@
 // leap year
-var d = new Date(2012, 01, 17, 01, 02, 03);
+var d = new Date(2012, 1, 17, 1, 2, 3);
 DateTools.getMonthDays(d) == 29;
 
 // seconds/delta