From: <fwi...@us...> - 2008-06-24 20:31:10
|
Revision: 4752 http://jython.svn.sourceforge.net/jython/?rev=4752&view=rev Author: fwierzbicki Date: 2008-06-24 13:31:06 -0700 (Tue, 24 Jun 2008) Log Message: ----------- Backing up to a 2 pass again -- but I think this may become a simpler two pass in time. Modified Paths: -------------- trunk/sandbox/wierzbicki/backup/Python.g Added Paths: ----------- trunk/sandbox/wierzbicki/backup/PythonWalker.g Modified: trunk/sandbox/wierzbicki/backup/Python.g =================================================================== --- trunk/sandbox/wierzbicki/backup/Python.g 2008-06-24 17:10:39 UTC (rev 4751) +++ trunk/sandbox/wierzbicki/backup/Python.g 2008-06-24 20:31:06 UTC (rev 4752) @@ -78,7 +78,7 @@ INDENT; DEDENT; - Module; + ModuleTok; Interactive; Expression; ExprTok; @@ -187,6 +187,10 @@ } @members { + //XXX: only used for single_input -- seems kludgy. + public boolean inSingle = false; + private boolean seenSingleOuterSuite = false; + boolean debugOn = false; private void debug(String message) { @@ -215,7 +219,12 @@ if (stmts != null) { List<stmtType> result = new ArrayList<stmtType>(); for (int i=0; i<stmts.size(); i++) { - result.add((stmtType)stmts.get(i)); + Object o = stmts.get(i); + if (o instanceof stmtType) { + result.add((stmtType)stmts.get(i)); + } else { + result.add((stmtType)((stmt_return)o).tree); + } } return (stmtType[])result.toArray(new stmtType[result.size()]); } @@ -330,7 +339,7 @@ } long l = Long.valueOf(s, radix).longValue(); - if (l > 0xffffffffl || (radix == 10 && l > Integer.MAX_VALUE)) { + if (l > 0xffffffffl || (l > Integer.MAX_VALUE)) { return Py.newLong(new BigInteger(s, radix)); } return Py.newInteger((int) l); @@ -422,6 +431,14 @@ throw e; } + protected Object recoverFromMismatchedToken(IntStream input, int ttype, BitSet follow) + throws RecognitionException + { + mismatch(input, ttype, follow); + return null; + } + + /** * A list holding the error message(s) encountered during parse. */ @@ -471,6 +488,8 @@ * a = [3, * 4] */ +//XXX: Hopefully we can remove inSingle when we get PyCF_DONT_IMPLY_DEDENT support. +public boolean inSingle = false; int implicitLineJoiningLevel = 0; int startPos=-1; @@ -503,20 +522,15 @@ } //single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE -//XXX: I don't know why, but in "compound_stmt NEWLINE" -// Jython chokes on the NEWLINE every time -- so I made -// it optional for now. -single_input : NEWLINE +single_input : NEWLINE -> ^(Interactive) | simple_stmt -> ^(Interactive simple_stmt) - | compound_stmt NEWLINE? -> ^(Interactive compound_stmt) + | compound_stmt NEWLINE -> ^(Interactive compound_stmt) ; //file_input: (NEWLINE | stmt)* ENDMARKER -file_input returns [modType mod] - : (NEWLINE | s+=stmt)+ { - $mod = new Module($file_input.start, makeStmts($s)); - } - | { $mod = new Module($file_input.start, new stmtType[0]); } +file_input + : (NEWLINE | s+=stmt)+ -> ^(ModuleTok<Module>[$file_input.start, makeStmts($s)]) + | -> ^(ModuleTok<Module>[$file_input.start, new stmtType[0\]]) ; //eval_input: testlist NEWLINE* ENDMARKER @@ -1370,7 +1384,11 @@ * Frank Wierzbicki added: Also ignore FORMFEEDS (\u000C). */ NEWLINE - : (('\u000C')?('\r')? '\n' )+ + : {inSingle}? => (('\u000C')?('\r')? '\n' ) + {if (implicitLineJoiningLevel>0 ) + $channel=HIDDEN; + } + | (('\u000C')?('\r')? '\n' )+ {if ( startPos==0 || implicitLineJoiningLevel>0 ) $channel=HIDDEN; } Added: trunk/sandbox/wierzbicki/backup/PythonWalker.g =================================================================== --- trunk/sandbox/wierzbicki/backup/PythonWalker.g (rev 0) +++ trunk/sandbox/wierzbicki/backup/PythonWalker.g 2008-06-24 20:31:06 UTC (rev 4752) @@ -0,0 +1,128 @@ +/* +Copyright (c) 2007-2008 Frank Wierzbicki +Licensed to PSF under a Contributor Agreement. +*/ +tree grammar PythonWalker; + +options { + output=AST; + tokenVocab=Python; + ASTLabelType=PythonTree; + rewrite=true; +} + +@header { +package org.python.antlr; + +import org.python.core.Py; +import org.python.core.PyObject; +import org.python.core.PyString; +import org.python.antlr.ParseException; +import org.python.antlr.ast.aliasType; +import org.python.antlr.ast.argumentsType; +import org.python.antlr.ast.boolopType; +import org.python.antlr.ast.comprehensionType; +import org.python.antlr.ast.cmpopType; +import org.python.antlr.ast.excepthandlerType; +import org.python.antlr.ast.exprType; +import org.python.antlr.ast.expr_contextType; +import org.python.antlr.ast.keywordType; +import org.python.antlr.ast.modType; +import org.python.antlr.ast.operatorType; +import org.python.antlr.ast.sliceType; +import org.python.antlr.ast.stmtType; +import org.python.antlr.ast.unaryopType; +import org.python.antlr.ast.Assert; +import org.python.antlr.ast.Assign; +import org.python.antlr.ast.Attribute; +import org.python.antlr.ast.AugAssign; +import org.python.antlr.ast.BinOp; +import org.python.antlr.ast.BoolOp; +import org.python.antlr.ast.Break; +import org.python.antlr.ast.Call; +import org.python.antlr.ast.ClassDef; +import org.python.antlr.ast.Compare; +import org.python.antlr.ast.Continue; +import org.python.antlr.ast.Delete; +import org.python.antlr.ast.Dict; +import org.python.antlr.ast.Ellipsis; +import org.python.antlr.ast.Exec; +import org.python.antlr.ast.Expr; +import org.python.antlr.ast.Expression; +import org.python.antlr.ast.ExtSlice; +import org.python.antlr.ast.For; +import org.python.antlr.ast.FunctionDef; +import org.python.antlr.ast.GeneratorExp; +import org.python.antlr.ast.Global; +import org.python.antlr.ast.If; +import org.python.antlr.ast.IfExp; +import org.python.antlr.ast.Index; +import org.python.antlr.ast.Import; +import org.python.antlr.ast.ImportFrom; +import org.python.antlr.ast.Interactive; +import org.python.antlr.ast.Lambda; +import org.python.antlr.ast.ListComp; +import org.python.antlr.ast.Module; +import org.python.antlr.ast.Name; +import org.python.antlr.ast.Num; +import org.python.antlr.ast.Slice; +import org.python.antlr.ast.Subscript; +import org.python.antlr.ast.TryExcept; +import org.python.antlr.ast.TryFinally; +import org.python.antlr.ast.Tuple; +import org.python.antlr.ast.Pass; +import org.python.antlr.ast.Print; +import org.python.antlr.ast.Raise; +import org.python.antlr.ast.Repr; +import org.python.antlr.ast.Return; +import org.python.antlr.ast.Str; +import org.python.antlr.ast.UnaryOp; +import org.python.antlr.ast.With; +import org.python.antlr.ast.While; +import org.python.antlr.ast.Yield; + +import java.math.BigInteger; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.ListIterator; +import java.util.Map; +import java.util.Set; +} +@members { + boolean debugOn = false; + + public void debug(String message) { + if (debugOn) { + System.out.println(message); + } + } + + protected void mismatch(IntStream input, int ttype, BitSet follow) throws RecognitionException { + throw new MismatchedTokenException(ttype, input); + } + + protected void mismatch(IntStream input, RecognitionException e, BitSet follow) throws RecognitionException { + throw e; + } +} + +@rulecatch { +catch (RecognitionException r) { + throw new ParseException(r); +} +} + +module + : ModuleTok + ; + +expression + : Expression + ; + +interactive + : Interactive + ; + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |