changeset 16:2a30281ef47c

finish number literals; add long string literal; git-svn-id: https://luan-java.googlecode.com/svn/trunk@17 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Thu, 29 Nov 2012 10:36:38 +0000
parents bb59d7ea223b
children 09d41f7490a8
files src/luan/interp/LuaParser.java
diffstat 1 files changed, 85 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
diff -r bb59d7ea223b -r 2a30281ef47c src/luan/interp/LuaParser.java
--- a/src/luan/interp/LuaParser.java	Thu Nov 29 07:59:17 2012 +0000
+++ b/src/luan/interp/LuaParser.java	Thu Nov 29 10:36:38 2012 +0000
@@ -21,7 +21,23 @@
 
 
 public class LuaParser extends BaseParser<Object> {
-	static final Object PAREN = new Object();
+	int nEquals;
+	int parens = 0;
+
+	boolean nEquals(int n) {
+		nEquals = n;
+		return true;
+	}
+
+	boolean incParens() {
+		parens++;
+		return true;
+	}
+
+	boolean decParens() {
+		parens--;
+		return true;
+	}
 
 	public Rule Target() {
 		return Sequence(
@@ -277,8 +293,7 @@
 
 	Rule TableExpr() {
 		return Sequence(
-			'{', Spaces(),
-			push(PAREN),
+			'{', incParens(), Spaces(),
 			push( new ArrayList<TableExpr.Field>() ),
 			push( 1.0 ),  // counter
 			Optional(
@@ -289,9 +304,8 @@
 				),
 				Optional( FieldSep() )
 			),
-			'}',
+			'}', decParens(),
 			push( newTableExpr() ),
-			drop(1),
 			Spaces()
 		);
 	}
@@ -357,7 +371,7 @@
 		return Sequence(
 			FirstOf(
 				Sequence(
-					'(', Spaces(), push(PAREN), Expr(), ')', drop(1), Spaces(),
+					'(', incParens(), Spaces(), Expr(), ')', decParens(), Spaces(),
 					push(expr(pop())),
 					push(null)  // marker
 				),
@@ -392,7 +406,7 @@
 		return Sequence(
 			FirstOf(
 				Sequence(
-					'(', Spaces(), push(PAREN), Expressions(), ')', drop(1), Spaces()
+					'(', incParens(), Spaces(), Expressions(), ')', decParens(), Spaces()
 				),
 				Sequence(
 					TableExpr(),
@@ -439,7 +453,7 @@
 	}
 
 	Rule SubExpr() {
-		return Sequence( '[', Spaces(), push(PAREN), Expr(), ']', drop(1), Spaces() );
+		return Sequence( '[', incParens(), Spaces(), Expr(), ']', decParens(), Spaces() );
 	}
 
 	Rule Name() {
@@ -533,8 +547,7 @@
 		return FirstOf(
 			Sequence(
 				IgnoreCase("0x"),
-				OneOrMore(HexDigit()),
-				push((double)Long.parseLong(match(),16))
+				HexNumber()
 			),
 			Sequence(
 				DecNumber(),
@@ -566,6 +579,45 @@
 		return OneOrMore(Digit());
 	}
 
+	Rule Digit() {
+		return CharRange('0', '9');
+	}
+
+	Rule HexNumber() {
+		return FirstOf(
+			Sequence(
+				HexInt(),
+				push( (double)Long.parseLong(match(),16) ),
+				Optional( '.', Optional(HexDec()) ),
+				HexExponent()
+			),
+			Sequence( push(0.0), '.', HexDec(), HexExponent() )
+		);
+	}
+
+	Rule HexDec() {
+		return Sequence(
+			HexInt(),
+			push( (Double)pop() + (double)Long.parseLong(match(),16) / Math.pow(16,matchLength()) )
+		);
+	}
+
+	Rule HexExponent() {
+		return Optional(
+			IgnoreCase('p'),
+			Sequence(
+				Optional(AnyOf("+-")),
+				HexInt()
+			),
+			push( (Double)pop() * Math.pow(2,(double)Long.parseLong(match())) )
+		);
+	}
+
+	Rule HexInt() {
+		return OneOrMore(Digit());
+	}
+
+
 	Rule HexDigit() {
 		return FirstOf(
 			Digit(),
@@ -573,14 +625,26 @@
 		);
 	}
 
-	Rule Digit() {
-		return CharRange('0', '9');
-	}
-
 	Rule StringLiteral() {
 		return FirstOf(
 			QuotedString('"'),
-			QuotedString('\'')
+			QuotedString('\''),
+			LongString()
+		);
+	}
+
+	Rule LongString() {
+		return Sequence(
+			"[",
+			ZeroOrMore('='),
+			nEquals(matchLength()),
+			'[',
+			ZeroOrMore(
+				TestNot(LongBracketsEnd()),
+				ANY
+			),
+			push( match() ),
+			LongBracketsEnd()
 		);
 	}
 
@@ -649,38 +713,27 @@
 				AnyOf(" \t"),
 				Comment(),
 				Sequence( '\\', EndOfLine() ),
-				Sequence( AnyOf("\r\n"), inParen() )
+				Sequence( AnyOf("\r\n"), parens > 0 )
 			)
 		);
 	}
 
-	boolean inParen() {
-		ValueStack stack = getContext().getValueStack();
-		int size = stack.size();
-		for( int i=0; i<size; i++ ) {
-			if( peek(i) == PAREN )
-				return true;
-		}
-		return false;
-	}
-
 	Rule Comment() {
-		Var<Integer> n = new Var<Integer>();
 		return Sequence(
 			"--[",
 			ZeroOrMore('='),
-			n.set(matchLength()),
+			nEquals(matchLength()),
 			'[',
 			ZeroOrMore(
-				TestNot(CommentEnd(n)),
+				TestNot(LongBracketsEnd()),
 				ANY
 			),
-			CommentEnd(n)
+			LongBracketsEnd()
 		);
 	}
 
-	Rule CommentEnd(Var<Integer> n) {
-		return Sequence( ']', ZeroOrMore('='), n.get()==matchLength(), ']' );
+	Rule LongBracketsEnd() {
+		return Sequence( ']', ZeroOrMore('='), nEquals==matchLength(), ']' );
 	}
 
 	// for debugging