aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--calculator-javacc/Calculator.jj53
1 files changed, 30 insertions, 23 deletions
diff --git a/calculator-javacc/Calculator.jj b/calculator-javacc/Calculator.jj
index 89a5802..b88937d 100644
--- a/calculator-javacc/Calculator.jj
+++ b/calculator-javacc/Calculator.jj
@@ -14,29 +14,33 @@ public class Calculator {
while (true){
System.out.print("Type your expression: ");
try{
- double value = parser.evaluateNextLine();
+
+ Double value = parser.evaluateNextLine();
System.out.println("Returned: " + value);
+
}catch (ParseException e){ // Now we will parse the exception message to render it in our format.
- int index = "Encountered: \"".length();
- String em = e.getMessage().substring(index);
- String token = em.substring(1,4);
-
- if (token.equals("ILL")){ // Illegal token
- em = em.substring(7);
- index = em.indexOf("\"")-1;
- if (index == 0){ // Special case in which the illegal token would be '"'
- index = 2;
- }
- System.out.println("Illegal token: '"+ em.substring(0 , index) +"'.");
- }else if(em.split("\n").length > 1){ //Other generated error messages
- index = em.indexOf("\"");
- if (index == 0){ // Meaning the last token is not <TOK> but "char"
- em = em.substring(1);
+ if(e.getMessage().split("\n").length > 1){ // The error message is on multiple line -> it had been generated
+ int index = "Encountered: \"".length();
+ String em = e.getMessage().substring(index);
+ String token = em.substring(1,4);
+
+ if (token.equals("ILL")){ // Illegal token
+ em = em.substring(7);
+ index = em.indexOf("\"")-1;
+ if (index == 0){ // Special case in which the illegal token would be '"'
+ index = 2;
+ }
+ System.out.println("Illegal token: '"+ em.substring(0 , index) +"'.");
+ }else{ //Other generated error messages
index = em.indexOf("\"");
- }else{
- index--;
+ if (index == 0){ // Meaning the last token is not <TOK> but "char"
+ em = em.substring(1);
+ index = em.indexOf("\"");
+ }else{
+ index--;
+ }
+ System.out.println("Last read token: '"+ em.substring(0 , index)+"', w"+em.substring(em.indexOf("\nW")+2));
}
- System.out.println("Last read token: '"+ em.substring(0 , index)+"', w"+em.substring(em.indexOf("\nW")+2));
}else{ //Those were error messages created by ourselves, they are one line messages that we can directly print
System.out.println(e.getMessage());
}
@@ -140,7 +144,7 @@ double factor() :
double i = 0.0;
}
{
- (t=<NUM> | id=<ID> | <PAL> (i=expression())? <PAR>)
+ (t=<NUM> | id=<ID> | <PAL> (i=expression()) <PAR>)
{
if(id != null){
Object var = variables.get(id.image);
@@ -152,7 +156,7 @@ double factor() :
i = Double.parseDouble(t.image);
return i;
}|
- <SUB> (t=<NUM> | id=<ID> | <PAL> (i=expression())? <PAR>)
+ <SUB> (t=<NUM> | id=<ID> | <PAL> (i=expression()) <PAR>)
{
if(id != null){
i = (double)variables.get(id.image);
@@ -167,7 +171,9 @@ double evaluateNextLine() :
double result = 0.0;
}
{
- (result=statement())? <EOL> {return result;}|
+ (result=statement()) <EOL> {
+ return result;
+ }|
<EOF>{System.out.println("\nBuffer was closed. Exiting."); System.exit(0);}
}
@@ -177,5 +183,6 @@ void passStatement() :
}
{
(<ILL>|<PAL>|<PAR>|<ADD>|<SUB>|<MUL>|<DIV>|<EQU>|<LET>|<END>|<NUM>|<ID>)* <EOL>| //Skip any token until the next EOL
- <EOF>{System.out.println("\nBuffer was closed. Exiting."); System.exit(0);}
+ <EOF>{System.out.println("\nBuffer was closed. Exiting."); System.exit(0);}|
+ {}
} \ No newline at end of file