From 7d4dfd4ad3cbe7c778fbed4fc00262f62a3bb8b2 Mon Sep 17 00:00:00 2001 From: "rogerl%netscape.com" Date: Tue, 27 Apr 1999 16:22:20 +0000 Subject: [PATCH] # Not a part of SeaMonkey git-svn-id: svn://10.0.0.236/trunk@29435 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/js/js2/java/Brenda.java | 97 ++++--------------------- mozilla/js/js2/java/JSGrammar.g | 52 +++++++------ mozilla/js/js2/java/RelationalNode.java | 7 ++ mozilla/js/js2/java/UnaryNode.java | 10 +++ 4 files changed, 60 insertions(+), 106 deletions(-) create mode 100644 mozilla/js/js2/java/RelationalNode.java diff --git a/mozilla/js/js2/java/Brenda.java b/mozilla/js/js2/java/Brenda.java index 41cc5909796..178dd390545 100644 --- a/mozilla/js/js2/java/Brenda.java +++ b/mozilla/js/js2/java/Brenda.java @@ -1,87 +1,16 @@ - +import java.io.*; class Brenda { - - static FunctionNode tf; - - public static void main(String cmdArgs[]) - { - IRFactory irf = new IRFactory(null); // only need a tokenstream for errors - - Object scriptBlock = irf.createLeaf(TokenStream.BLOCK); // will hold the global level stuff -/* - - this constructs a function 'TestFunction' containing 'foo = 0; throw a; return foo;' - -*/ - Object funcBlock = irf.createLeaf(TokenStream.BLOCK); - Object nameFoo1 = irf.createName("foo"); - Object zero = irf.createNumber(new Double(0)); - Object asgn1 = irf.createAssignment(TokenStream.NOP, (Node)nameFoo1, (Node)zero, null, false); - Object exprStmt = irf.createExprStatement(asgn1, 0); - irf.addChildToBack(funcBlock, exprStmt); - - Object nameA2 = irf.createName("a"); - Object throwStmt = irf.createThrow(nameA2, 0); - irf.addChildToBack(funcBlock, throwStmt); - - Object nameFoo2 = irf.createName("foo"); - Object rtrnStmt = irf.createReturn(nameFoo2, 0); - irf.addChildToBack(funcBlock, rtrnStmt); - - Object args = irf.createLeaf(TokenStream.LP); - Object function = irf.createFunction("TestFunction", args, funcBlock, "SourceName", 0, 0, null); - tf = (FunctionNode)(((Node)function).getProp(Node.FUNCTION_PROP)); - irf.addChildToBack(scriptBlock, function); - -/* - a = 1.0 -*/ - Object nameA = irf.createName("a"); - Object number1 = irf.createNumber(new Double(1)); - Object setNameA = irf.createAssignment(TokenStream.NOP, (Node)nameA, (Node)number1, null, false); - Object exprStmt2 = irf.createExprStatement(setNameA, 0); - irf.addChildToBack(scriptBlock, exprStmt2); - -/* - try { - c = TestFunction() - } - catch (e) { - b = 2.0 - } -*/ - Object nameC = irf.createName("c"); - Object nameTF = irf.createName("TestFunction"); - Object funCall = irf.createUnary(TokenStream.CALL, nameTF); - Object setNameC = irf.createAssignment(TokenStream.NOP, (Node)nameC, (Node)funCall, null, false); - Object exprStmt3 = irf.createExprStatement(setNameC, 0); - Object tryBlock = irf.createLeaf(TokenStream.BLOCK); - irf.addChildToBack(tryBlock, exprStmt3); - - - Object nameB = irf.createName("b"); - Object number2 = irf.createNumber(new Double(2)); - Object setNameB = irf.createAssignment(TokenStream.NOP, (Node)nameB, (Node)number2, null, false); - Object exprStmt4 = irf.createExprStatement(setNameB, 0); - Object catchClause = irf.createCatch("e", null, exprStmt4, 0); - Object catchBlock = irf.createLeaf(TokenStream.BLOCK); - irf.addChildToBack(catchBlock, catchClause); - - Object tryStmt = irf.createTryCatchFinally(tryBlock, catchBlock, null, 0); - - irf.addChildToBack(scriptBlock, tryStmt); - - - - Object script = irf.createScript(scriptBlock, "SourceName", 0, 0, null); - - - System.out.println(((Node)script).toStringTree()); - - Interpreter interp = new Interpreter(); - interp.executeScript((Node)script); - - } - + + public static void main(String[] args) { + try { + JSLexer lexer = new JSLexer((args != null) ? new DataInputStream(new FileInputStream(args[0])) : new DataInputStream(System.in)); + JSParser parser = new JSParser(lexer); + ExpressionNode tree = parser.expression(true, true); + System.out.println(tree.print("")); + } catch(Exception e) { + System.err.println("exception: "+e); + } + } + } \ No newline at end of file diff --git a/mozilla/js/js2/java/JSGrammar.g b/mozilla/js/js2/java/JSGrammar.g index 7c3734bf948..c83fc59d104 100644 --- a/mozilla/js/js2/java/JSGrammar.g +++ b/mozilla/js/js2/java/JSGrammar.g @@ -125,7 +125,7 @@ postfix_expression[boolean initial] returns [ExpressionNode e] | e = new_expression ) ( - r = member_operator { e = new BinaryNode(".", e, r); } + r = member_operator { e = new BinaryNode(((UnaryNode)r).getOperator(), e, ((UnaryNode)r).getChild()); } | r = arguments { e = new BinaryNode("()", e, r); } | "++" { e = new UnaryNode("++", e); } | "--" { e = new UnaryNode("--", e); } @@ -224,17 +224,18 @@ shift_expression[boolean initial] returns [ExpressionNode e] ; // ********* Relational Operators ********** -relational_operator[boolean allowIn] - : {allowIn}? "in" - | "<" - | ">" - | "<=" - | ">=" - | "instanceof" +relational_operator[boolean allowIn] returns [String s] + { s = null; } + : {allowIn}? "in" { s = "in"; } + | "<" { s = "<"; } + | ">" { s = ">"; } + | "<=" { s = "<="; } + | ">=" { s = ">="; } + | "instanceof" { s = "instanceof"; } ; relational_expression[boolean initial, boolean allowIn] returns [ExpressionNode e] - { e = null; } + { e = null; ExpressionNode r = null; String op = null; } : e = shift_expression[initial] ( // ANTLR reports an ambiguity here because the FOLLOW set of a relational_expression @@ -242,45 +243,52 @@ relational_expression[boolean initial, boolean allowIn] returns [ExpressionNode // here because the 'allowIn' semantic predicate is used to prevent the two from being // confused. options { warnWhenFollowAmbig=false; }: - relational_operator[allowIn] shift_expression[false] + op = relational_operator[allowIn] r = shift_expression[false] { e = new RelationalNode(op, e, r); } )* ; // ********* Equality Operators ********** equality_expression[boolean initial, boolean allowIn] returns [ExpressionNode e] - { e = null; } + { e = null; ExpressionNode r = null; } : e = relational_expression[initial, allowIn] - (("==" | "!=" | "===" | "!==") relational_expression[false, allowIn])* + ( + ("==" r = relational_expression[false, allowIn]) { e = new RelationalNode("==", e, r); } + | ("!=" r = relational_expression[false, allowIn]) { e = new RelationalNode("!=", e, r); } + | ("===" r = relational_expression[false, allowIn]) { e = new RelationalNode("===", e, r); } + | ("!==" r = relational_expression[false, allowIn]) { e = new RelationalNode("!===", e, r); } + )* ; // ********* Binary Bitwise Operators ********** bitwise_and_expression[boolean initial, boolean allowIn] returns [ExpressionNode e] - { e = null; } + { e = null; ExpressionNode r = null; } : e = equality_expression[initial, allowIn] - ("&" equality_expression[false, allowIn])* + ("&" r = equality_expression[false, allowIn] { e = new ArithmeticNode("&", e, r); } )* ; bitwise_xor_expression[boolean initial, boolean allowIn] returns [ExpressionNode e] - { e = null; } + { e = null; ExpressionNode r = null; } : e = bitwise_and_expression[initial, allowIn] - ("^" (bitwise_and_expression[false, allowIn] | "*" | "?"))* + ("^" (r = bitwise_and_expression[false, allowIn] { e = new BinaryNode("^", e, r); } | "*" | "?"))* ; bitwise_or_expression[boolean initial, boolean allowIn] returns [ExpressionNode e] - { e = null; } + { e = null; ExpressionNode r = null; } : e = bitwise_xor_expression[initial, allowIn] - ("|" (bitwise_xor_expression[false, allowIn] | "*" | "?"))* + ("|" (r = bitwise_xor_expression[false, allowIn] { e = new BinaryNode("|", e, r); } | "*" | "?"))* ; // ********* Binary Logical Operators ********** logical_and_expression[boolean initial, boolean allowIn] returns [ExpressionNode e] - { e = null; } - : e = bitwise_or_expression[initial, allowIn] ("&&" bitwise_or_expression[false, allowIn])* + { e = null; ExpressionNode r = null; } + : e = bitwise_or_expression[initial, allowIn] + ("&&" r = bitwise_or_expression[false, allowIn] { e = new BinaryNode("&&", e, r); } )* ; logical_or_expression[boolean initial, boolean allowIn] returns [ExpressionNode e] - { e = null; } - : e = logical_and_expression[initial, allowIn] ("||" logical_and_expression[false, allowIn])* + { e = null; ExpressionNode r = null; } + : e = logical_and_expression[initial, allowIn] + ("||" r = logical_and_expression[false, allowIn] { e = new BinaryNode("||", e, r); } )* ; // ********* Conditional Operators ********** diff --git a/mozilla/js/js2/java/RelationalNode.java b/mozilla/js/js2/java/RelationalNode.java new file mode 100644 index 00000000000..ffec2f35f21 --- /dev/null +++ b/mozilla/js/js2/java/RelationalNode.java @@ -0,0 +1,7 @@ +class RelationalNode extends BinaryNode { + + RelationalNode(String aOp, ExpressionNode aLeft, ExpressionNode aRight) + { + super(aOp, aLeft, aRight); + } +} diff --git a/mozilla/js/js2/java/UnaryNode.java b/mozilla/js/js2/java/UnaryNode.java index f97fc80bbf5..102c8b8f1d6 100644 --- a/mozilla/js/js2/java/UnaryNode.java +++ b/mozilla/js/js2/java/UnaryNode.java @@ -22,6 +22,16 @@ class UnaryNode extends ExpressionNode { return result.toString(); } + String getOperator() + { + return op; + } + + ExpressionNode getChild() + { + return child; + } + protected ExpressionNode child; protected String op;