Commit 6768c93b authored by Sebastian Pretscher's avatar Sebastian Pretscher
Browse files

Merge branch 'master' of git@github.com:jroith/cup-eclipse.git

Conflicts:
	CupPlugin/src/de/tum/in/www2/cupplugin/model/CupParserASTChangeObserver.java
parents 555e511f 70ab400c
......@@ -14,7 +14,7 @@
<target name="generate">
<jflex file="${jflex}/Lexer.jflex" destdir="${java}" />
<cup srcfile="${cup}/Parser.cup" destdir="${java}"
parser="Parser" interface="true" locations="false" />
parser="Parser" interface="true" locations="true" />
</target>
<path id="libraries"> <files includes="${lib}/java-cup-11b-runtime.jar" /> </path>
......@@ -31,6 +31,10 @@
<manifest>
</manifest>
</jar>
<echo>Deploy in </echo>
<copy todir="../CupReferencedLibraries/" overwrite="true">
<fileset dir="${result}/" includes="*.jar"/>
</copy>
</target>
<target name="clean">
......
......@@ -204,7 +204,10 @@ action code {:
protected void ast_add_precedence(ComplexSymbol head, Object lst)
{
Precedence p = new Precedence(_cur_side, (List<Terminal>) lst,
head, (ComplexSymbol) stack.peek());
new Range(Position.fromComplexSymbolLeft(head),
Position.fromComplexSymbolRight((ComplexSymbol) stack.peek()
)
));
parserResult.precedences.add(p);
}
......@@ -241,18 +244,18 @@ parser code {:
{
done_parsing();
if (info instanceof Symbol)
errMan.Fatal(message+ "\nCan't recover from previous error(s), giving up.",(Symbol)info);
errMan.Fatal(ErrorSource.Parser, message + "\nCan't recover from previous error(s), giving up.",(ComplexSymbol)info);
else
errMan.Fatal(message + "\nCan't recover from previous error(s), giving up.",cur_token);
errMan.Fatal(ErrorSource.Parser, message + "\nCan't recover from previous error(s), giving up.",(ComplexSymbol)cur_token);
throw new ParserAbortException();
}
public void report_error(String message, Object info)
{
if (info instanceof Symbol)
errMan.Error(message,(Symbol)info);
errMan.Error(ErrorSource.Parser, message, (ComplexSymbol) info);
else
errMan.Error(message,cur_token);
errMan.Error(ErrorSource.Parser, message, (ComplexSymbol) cur_token);
}
:};
......@@ -330,18 +333,16 @@ spec ::=
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
package_spec ::=
PACKAGE {:
head_tokens.push((ComplexSymbol) stack.peek());
:}
PACKAGE : p
multipart_id
{:
/* save the package name */
context.package_name = multipart_name;
parserResult.pack = new de.in.tum.www2.cup.ast.Package(
new Name (multipart_name, null, null), // TODO: from, to
head_tokens.pop(),
(ComplexSymbol) cur_token);
new Name (multipart_name, null), // TODO: from, to
new Range(Position.fromLocation(pxleft),
Position.fromComplexSymbolRight((ComplexSymbol) cur_token)));
/* reset the accumulated multipart name */
multipart_name = new String();
......@@ -372,9 +373,9 @@ import_spec ::=
context.import_list.push(multipart_name);
Import imprt = new Import(
new Name (multipart_name, null, null), // TODO: from, to
head_tokens.pop(),
(ComplexSymbol) cur_token);
new Name (multipart_name, null), // TODO: from, to
new Range(Position.fromComplexSymbolLeft(head_tokens.pop()),
Position.fromComplexSymbolRight((ComplexSymbol) cur_token)));
parserResult.imports.add(imprt);
/* reset the accumulated multipart name */
......@@ -393,9 +394,9 @@ class_name ::=
context.symbol_const_class_name = id + "Sym";
parserResult.className = new ClassName (
new Name(id, null, null), //jrTODO: positions
head_tokens.pop(),
(ComplexSymbol) stack.peek());
new Name(id, null), //jrTODO: positions
new Range(Position.fromComplexSymbolLeft(head_tokens.pop()),
Position.fromComplexSymbolRight((ComplexSymbol) stack.peek())));
:}
;
......@@ -417,12 +418,16 @@ action_code_part ::=
:} CODE CODE_STRING : user_code opt_semi
{:
if (context.action_code!=null)
errMan.Warning("Redundant action code (skipping)");
errMan.Warning(ErrorSource.Parser,
"Redundant action code (skipping)",
Position.fromLocation(user_codexleft));
else { /* save the user included code string */
context.action_code = user_code;
parserResult.actionCode = new CodeBlock(user_code,
head_tokens.pop(),
(ComplexSymbol) cur_token); // jrTODO!
parserResult.actionCode = new SpecialCodeBlock(
SpecialCodeBlock.BlockType.Action,
user_code,
new Range(Position.fromComplexSymbolLeft(head_tokens.pop()),
Position.fromComplexSymbolRight((ComplexSymbol) cur_token))); // jrTODO!
}
:}
;
......@@ -435,12 +440,16 @@ parser_code_part ::=
:} CODE CODE_STRING:user_code opt_semi
{:
if (context.parser_code!=null)
errMan.Warning("Redundant parser code (skipping)");
errMan.Warning(ErrorSource.Parser,
"Redundant parser code (skipping)",
Position.fromLocation(user_codexleft));
else { /* save the user included code string */
context.parser_code = user_code;
parserResult.parserCode = new CodeBlock(user_code,
head_tokens.pop(),
(ComplexSymbol) cur_token); // jrTODO!
parserResult.parserCode = new SpecialCodeBlock(
SpecialCodeBlock.BlockType.Parser,
user_code,
new Range(Position.fromComplexSymbolLeft(head_tokens.pop()),
Position.fromComplexSymbolRight((ComplexSymbol) cur_token))); // jrTODO!
}
:}
;
......@@ -453,12 +462,15 @@ init_code ::=
:} WITH CODE_STRING:user_code opt_semi
{:
if (context.init_code!=null)
errMan.Warning("Redundant init code (skipping)");
errMan.Warning(ErrorSource.Parser, "Redundant init code (skipping)",
Position.fromLocation(user_codexleft));
else {/* save the user code */
context.init_code = user_code;
parserResult.initCode = new CodeBlock(user_code,
head_tokens.pop(),
(ComplexSymbol) cur_token); // jrTODO!
parserResult.initCode = new SpecialCodeBlock(
SpecialCodeBlock.BlockType.Init,
user_code,
new Range(Position.fromComplexSymbolLeft(head_tokens.pop()),
Position.fromComplexSymbolRight((ComplexSymbol) cur_token))); // jrTODO!
}
:}
;
......@@ -471,13 +483,16 @@ scan_code ::=
:} WITH CODE_STRING:user_code opt_semi
{:
if (context.scan_code!=null)
errMan.Warning("Redundant scan code (skipping)");
errMan.Warning(ErrorSource.Parser, "Redundant scan code (skipping)",
Position.fromLocation(user_codexleft));
else {
/* save the user code */
context.scan_code = user_code;
parserResult.scanCode = new CodeBlock(user_code,
head_tokens.pop(),
(ComplexSymbol) cur_token); // jrTODO!
parserResult.scanCode = new SpecialCodeBlock(
SpecialCodeBlock.BlockType.Scan,
user_code,
new Range(Position.fromComplexSymbolLeft(head_tokens.pop()),
Position.fromComplexSymbolRight((ComplexSymbol) cur_token))); // jrTODO!
}
:}
......@@ -501,8 +516,8 @@ symbol ::=
parserResult.symbols.add(new TypedTerminalDeclaration(
(String) tid,
(List<Terminal>) lst,
terminal,
(ComplexSymbol) stack.peek()));
new Range(Position.fromComplexSymbolLeft(terminal),
Position.fromComplexSymbolRight((ComplexSymbol) stack.peek()))));
:}
|
TERMINAL
......@@ -513,8 +528,8 @@ symbol ::=
parserResult.symbols.add(new TerminalDeclaration(
(List<Terminal>) lst,
terminal,
(ComplexSymbol) stack.peek()));
new Range(Position.fromComplexSymbolLeft(terminal),
Position.fromComplexSymbolRight((ComplexSymbol) stack.peek()))));
;
:}
|
......@@ -528,8 +543,8 @@ symbol ::=
parserResult.symbols.add(new TypedNonTerminalDeclaration(
(String) tid,
(List<NonTerminal>) lst,
nonTerminal,
(ComplexSymbol) stack.peek()));
new Range(Position.fromComplexSymbolLeft(nonTerminal),
Position.fromComplexSymbolRight((ComplexSymbol) stack.peek()))));
:}
|
non_terminal
......@@ -540,8 +555,8 @@ symbol ::=
ComplexSymbol nonTerminal = (ComplexSymbol) stack.elementAt(stack.size()-2);
parserResult.symbols.add(new NonTerminalDeclaration(
(List<NonTerminal>) lst,
nonTerminal,
(ComplexSymbol) stack.peek()));
new Range(Position.fromComplexSymbolLeft(nonTerminal),
Position.fromComplexSymbolRight((ComplexSymbol) stack.peek()))));
:}
|
......@@ -649,14 +664,14 @@ preced_type ::=
terminal_list ::= terminal_list:lst COMMA terminal_id:trm
{:
((List<Terminal>) lst).add(new Terminal(trm, null, null)); // jrTODO: positions
((List<Terminal>) lst).add(new Terminal(trm, null)); // jrTODO: positions
RESULT = lst;
:}
|
terminal_id:trm
{:
List<Terminal> lst = new ArrayList<Terminal>();
lst.add(new Terminal(trm, null, null)); // jrTODO: positions
lst.add(new Terminal(trm, null)); // jrTODO: positions
RESULT = lst;
:}
;
......@@ -677,8 +692,8 @@ term_id ::= symbol_id:sym
if (symbols.get(sym) == null)
{
/* issue a message */
errMan.Error("Terminal \"" + sym +
"\" has not been declared");
errMan.Error(ErrorSource.Parser, "Terminal \"" + sym +
"\" has not been declared", Position.fromLocation(symxleft));
}
RESULT = sym;
:};
......@@ -694,8 +709,9 @@ start_spec ::=
non_terminal nt = (non_terminal)non_terms.get(start_name);
if (nt == null)
{
errMan.Error( "Start non terminal \"" + start_name +
"\" has not been declared");
errMan.Error(ErrorSource.Parser,
"Start non terminal \"" + start_name + "\" has not been declared",
Position.fromLocation(start_namexleft));
}
else
{
......@@ -713,9 +729,9 @@ start_spec ::=
//jrTODO: position, start symbol?, side effects?
parserResult.startWith = new StartWith(
new NonTerminal (start_name, null, null),
head_tokens.pop(),
(ComplexSymbol) stack.peek());
new NonTerminal (start_name, null),
new Range(Position.fromComplexSymbolLeft(head_tokens.pop()),
Position.fromComplexSymbolRight((ComplexSymbol) stack.peek())));
}
:}
......@@ -733,10 +749,6 @@ production_list ::= production_list production | production;
production ::=
nt_id : lhs_id
{:
/* lookup the lhs nt */
lhs_nt = (non_terminal)non_terms.get(lhs_id);
......@@ -744,8 +756,9 @@ production ::=
if (lhs_nt == null)
{
if (errMan.getErrorCount() == 0)
errMan.Warning("LHS non terminal \"" + lhs_id +
"\" has not been declared");
errMan.Warning(ErrorSource.Parser, "LHS non terminal \"" + lhs_id +
"\" has not been declared",
Position.fromLocation(lhs_idxleft));
}
/* reset the rhs accumulation */
......@@ -754,16 +767,23 @@ production ::=
COLON_COLON_EQUALS
/* {: :}*/
rhs_list : rhs
SEMI{:
parserResult.productions.add(new Production(
new Name((String) lhs_id, null, null), // jrTODO: positions!
(List<ProductionRight>) rhs,
null,
null));
SEMI : s
{:
parserResult.productions.add(
new Production(
new Name(
(String) lhs_id,
Range.fromLocations(lhs_idxleft, lhs_idxright)
),
(List<ProductionRight>) rhs,
Range.fromLocations(lhs_idxleft, sxright)
)
);
:}
|
error
{: errMan.Error("Syntax Error"); :}
error : e {:
errMan.Error(ErrorSource.Parser, "Production Syntax Error", Position.fromLocation(exleft));
:}
SEMI
;
......@@ -790,10 +810,22 @@ rhs ::=
{
/* Find the precedence symbol */
if (term_name == null) {
// jrTODO: report proper error!
System.err.println("No terminal for contextual precedence");
sym = null;
} else {
sym = ((symbol_part)symbols.get(term_name)).the_symbol();
symbol_part sp = null;
try{
sp = (symbol_part) symbols.get(term_name);
} catch (NullPointerException e) {
// fix crash.
errMan.Error(ErrorSource.Parser,
"could not resolve symbol!",
Position.fromLocation(term_namexleft));
}
if (sp != null)
sym = sp.the_symbol();
}
/* build the production */
production p;
......@@ -844,7 +876,7 @@ rhs ::=
ast_new_rhs();
:}
|
prod_part_list
prod_part_list : ppl
{:
......@@ -853,7 +885,8 @@ rhs ::=
/* build the production */
production p = new production(context, lhs_nt, rhs_parts, rhs_pos);
RESULT = new ProductionRight(ast_get_rhs_parts_copy(), null, null);
RESULT = new ProductionRight(ast_get_rhs_parts_copy(),
Range.fromLocations(pplxleft, pplxright));
/* if we have no start non-terminal declared and this is
......@@ -901,18 +934,17 @@ prod_part ::=
/* if that fails, symbol is undeclared */
if (symb == null) {
if (errMan.getErrorCount() == 0)
errMan.Error("java_cup.runtime.Symbol \"" + symid +
"\" has not been declared");
errMan.Error(ErrorSource.Parser, "Symbol \"" + symid +
"\" has not been declared", Position.fromLocation(symidxleft));
}
else {
/* add a labeled production part */
add_rhs_part(add_lab(symb, labid));
// jrTODO: we need to check if the production really is labeled!
ast_add_rhs_part(new LabeledProductionRef(
new Name(symid, null, null), // jrTODO: locations
new Name(symid, null), // jrTODO: locations
labid,
null,
null)); // jrTODO: locations
}
:}
......@@ -921,7 +953,9 @@ prod_part ::=
{:
/* add a new production part */
add_rhs_part(new action_part(code_str));
ast_add_rhs_part(new ActionCodeBlock(code_str, null, null));
ast_add_rhs_part(new ActionCodeBlock(code_str,
Range.fromLocations(code_strxleft, code_strxright)
)); // jrTODO: positions
:}
;
......@@ -991,22 +1025,27 @@ import_id ::=
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
type_id ::= multipart_id
type_id ::= multipart_id {:
RESULT = multipart_name;
:}
| type_id LBRACK RBRACK
{: multipart_name = multipart_name.concat("[]"); :}
{:
multipart_name = multipart_name.concat("[]");
RESULT = multipart_name;
:}
;
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
new_term_id ::=
ID:term_id
ID : term_id
{:
/* see if this terminal has been declared before */
if (symbols.get(term_id) != null)
{
/* issue a message */
errMan.Error("java_cup.runtime.Symbol \"" + term_id +
"\" has already been declared");
errMan.Error(ErrorSource.Parser, "Symbol \"" + term_id +
"\" has already been declared", Position.fromLocation(term_idxleft));
}
else
{
......@@ -1019,11 +1058,7 @@ new_term_id ::=
symbols.put(term_id,
new symbol_part(new terminal(context, term_id, multipart_name)));
// jrTODO: positions!
// jrTODO: term_id vs. multipart_name
// -> multipart_name is really the tid, that we could not get properly before!!!!
RESULT = new Terminal(term_id + "_" + multipart_name, null, null);
RESULT = new Terminal(term_id, null);
}
:}
......@@ -1038,8 +1073,8 @@ new_non_term_id ::=
if (symbols.get(non_term_id) != null)
{
/* issue a message */
errMan.Error( "java_cup.runtime.Symbol \"" + non_term_id +
"\" has already been declared");
errMan.Error(ErrorSource.Parser, "Symbol \"" + non_term_id +
"\" has already been declared", Position.fromLocation(non_term_idxleft));
}
else
{
......@@ -1058,7 +1093,7 @@ new_non_term_id ::=
// jrTODO: positions!
// jrTODO: non_term_id vs. multipart_name
RESULT = new NonTerminal(non_term_id + "_" + multipart_name, null, null);
RESULT = new NonTerminal(non_term_id, null);
}
:}
;
......@@ -1068,9 +1103,11 @@ new_non_term_id ::=
nt_id ::=
ID:the_id
{: RESULT = the_id; :}
| error
| error : e
{:
errMan.Error("Illegal use of reserved word");
errMan.Error(ErrorSource.Parser,
"Illegal use of reserved word",
Position.fromLocation(exleft));
RESULT="ILLEGAL";
:}
;
......@@ -1080,9 +1117,11 @@ nt_id ::=
symbol_id ::=
ID:the_id
{: RESULT = the_id; :}
| error
| error : e
{:
errMan.Error("Illegal use of reserved word");
errMan.Error(ErrorSource.Parser,
"Illegal use of reserved word",
Position.fromLocation(exleft));
RESULT="ILLEGAL";
:}
;
......@@ -1114,9 +1153,11 @@ robust_id ::= /* all ids that aren't reserved words in Java */
| LEFT {: RESULT = "left"; :}
| RIGHT {: RESULT = "right"; :}
| NONASSOC {: RESULT = "nonassoc"; :}
| error
| error : e
{:
errMan.Error("Illegal use of reserved word");
errMan.Error(ErrorSource.Parser,
"Illegal use of reserved word",
Position.fromLocation(exleft));
RESULT="ILLEGAL";
:}
;
......
......@@ -20,8 +20,7 @@ public class CupContext
{
String name = cls.getName();
if (!singletons.containsKey(name)) {
System.out.println("setting up singleton for " + name + "!");
//System.out.println("setting up singleton for " + name + "!");
singletons.put(name, null); // just in case to prevent loops.
try {
Constructor<T> c = cls.getConstructor(
......@@ -48,17 +47,17 @@ public class CupContext
// TODO: this stuff is only needed for the emitter and can be easily removed!
// -> could also be extracted from the AST.
public static Stack<String> import_list = new Stack<String>();
public static String parser_class_name;
public static String package_name;
public static String action_code;
public static String parser_code;
public static String init_code;
public static String scan_code;
public static String symbol_const_class_name;
public Stack<String> import_list = new Stack<String>();
public String parser_class_name;
public String package_name;
public String action_code;
public String parser_code;
public String init_code;
public String scan_code;
public String symbol_const_class_name;
public static int num_conflicts;
public static production start_production;
public int num_conflicts;
public production start_production;
public Boolean getShowWarnings() {
return showWarnings;
......
......@@ -2,6 +2,7 @@
package de.in.tum.www2.cup;
import java.io.FileInputStream;
import java.io.InputStream;
import de.in.tum.www2.cup.ast.*;
import de.in.tum.www2.cup.internal.Parser;
......@@ -21,7 +22,7 @@ public class CupParser
return context;
}
public CupParser(IErrorReporter er, FileInputStream inputStream) {
public CupParser(IErrorReporter er, InputStream inputStream) {
context = new CupContext(er);
parser = new Parser();
parser.init(context.getErrorManager(), context, inputStream);
......@@ -33,6 +34,9 @@ public class CupParser
try {
parser.parse();
} catch (ParserAbortException e) {
System.out.println("PARSER ABORT!");
this.context = null;
return null;
}
......
......@@ -4,9 +4,10 @@ package de.in.tum.www2.cup;
import java.io.Reader;
import java.io.IOException;
import java.io.InputStream;
import de.in.tum.www2.cup.ErrorManager;
import java_cup.runtime.ComplexSymbolFactory;
import java_cup.runtime.ComplexSymbolFactory.ComplexSymbol;
import de.in.tum.www2.cup.internal.Lexer;
public class CupScanner
......@@ -25,8 +26,9 @@ public class CupScanner
this.lexer = new Lexer(errMan, factory, r);
}
public java_cup.runtime.Symbol next_token() throws IOException {
return lexer.next_token();
public CupSymbol next_token() throws IOException {
ComplexSymbol symbol = (ComplexSymbol) lexer.next_token();
return new CupSymbol(symbol);
}
}
......
package de.in.tum.www2.cup;
import java.util.HashSet;
import java_cup.runtime.ComplexSymbolFactory.ComplexSymbol;
import de.in.tum.www2.cup.internal.sym;
/**
* Wrapper class around symbols, so we can have a nice API.
*/
public class CupSymbol {
private ComplexSymbol cs;
public Object getValue() {
if (cs == null)
return null;
return cs.value;
}
public int getSymbolId() {