@ -1,6 +1,7 @@
package chocopy.pa1;
import java_cup.runtime.*;
import java.util.ArrayList;
import java.util.Iterator;
%%
/*** Do not change the flags below unless you know what you are doing. ***/
@ -37,6 +38,7 @@ import java.util.ArrayList;
private int str_l = 0, str_c = 0; //Start location of a string.
/*A stack that keeps track of the spaces in each Indentation Level*/
private ArrayList<Integer> stack = new ArrayList<Integer>(20);
private boolean indentErrorUnchecked = true;
/** Return a terminal symbol of syntactic category TYPE and no
* semantic value at the current source location. */
private Symbol symbol(int type) {
@ -63,6 +65,15 @@ import java.util.ArrayList;
if(stack.isEmpty()) return 0;
return stack.get(stack.size() - 1);
}
private boolean find(int indent){
if(indent == 0) return true;
Iterator<Integer> it = stack.iterator();
while(it.hasNext()){
if(it.next() == indent)
return true;
}
return false;
}
%}
/* Macros (regexes used in rules below) */
@ -72,7 +83,7 @@ LineBreak = \r|\n|\r\n
IntegerLiteral = 0|[1-9][0-9]* // Accroding to the manual, 00+ is illeagal
StringLiteral = ([^\"\\]|(\\\")|(\\t)|(\\r)|(\\n)|(\\\\))+ // \n, \r, \t, \\, \" and Anything except \ and "
Identifiers = (_|[a-z]|[A-Z])(_|[a-z]|[A-Z][0-9])*
Identifiers = (_|[a-z]|[A-Z])(_|[a-z]|[A-Z]| [0-9])*
Comments = #[^\r\n]*
%%
//YYINITIAL state is where we're dealing with indentations.
@ -135,7 +146,18 @@ if True:
AFTER state.
*/
pop();
return symbol(ChocoPyTokens.DEDENT, currIndent);
if(top() < currIndent)
{
currIndent = top();
return symbolFactory.newSymbol("<bad indentation>", ChocoPyTokens.UNRECOGNIZED,
new ComplexSymbolFactory.Location(yyline + 1, yycolumn - 1),
new ComplexSymbolFactory.Location(yyline + 1,yycolumn + yylength()),
currIndent);
}
return symbolFactory.newSymbol(ChocoPyTokens.terminalNames[ChocoPyTokens.DEDENT], ChocoPyTokens.DEDENT,
new ComplexSymbolFactory.Location(yyline + 1, yycolumn - 1),
new ComplexSymbolFactory.Location(yyline + 1,yycolumn + yylength()),
currIndent);
}
/*Otherwise, we will start dealing with the rest
of the line after indentation in AFTER state. */
@ -159,7 +181,7 @@ if True:
<AFTER> {
/* Delimiters. */
{LineBreak} { yybegin(YYINITIAL); currIndent = 0;return symbol(ChocoPyTokens.NEWLINE);}
{LineBreak} { yybegin(YYINITIAL); currIndent = 0;indentErrorUnchecked = true; return symbol(ChocoPyTokens.NEWLINE);}
":" { return symbol(ChocoPyTokens.COLON); }
"," { return symbol(ChocoPyTokens.COMMA); }