Skip to content

Commit

Permalink
parser: corrected reporting of errors in numbers
Browse files Browse the repository at this point in the history
there was an error condition that was not detected properly, which was
reported as "expected sign or digits for exponent in floating point
constant" but an exponent sign was present, so a new "expected digits
for exponent in floating point constant" error was added and the get
number function was modified to detect and throw this new error

changed the "expected digits in mantissa of floating point constant"
error was to point to both the '.' and 'E' in the incorrect ".E"

changed translator to request a reference token for the first token
added reference argument (default none) to translator get token
  (prevent number tokens when reference is not none)
changed translator get operand to pass reference flag to get token
  (which now reports appropriate error, so upon error just return status)

changed the "expected command" error to say "expected command or
item for assignment" (status enumerator also changed)

cleaned up the let translate error handling

updated parser test 3 (number tests) for changes in error messages
updated translator tests 1, 3 (assignments) and 14 for error changes
added few more tests to translator test 14 (parser) to test new error
updated several translator tests for expected command error change
  • Loading branch information
thunder422 committed Nov 1, 2014
1 parent 336ad07 commit 20e46cc
Show file tree
Hide file tree
Showing 22 changed files with 126 additions and 84 deletions.
21 changes: 8 additions & 13 deletions basic/let.cpp
Expand Up @@ -55,25 +55,20 @@ Status letTranslate(Translator &translator, TokenPtr commandToken,
if ((status = translator.getOperand(token, dataType,
Translator::Reference::All)) != Status::Good)
{
if (token->column() > column)
if (token->column() > column) // not at begin command?
{
return status;
return status; // return error as is
}
// next token determines error
TokenPtr nextToken;
if ((status = translator.getToken(nextToken)) != Status::Good)
{
status = Status::ExpCmd;
}
if (nextToken->isCode(Comma_Code) || nextToken->isCode(Eq_Code))
{
status = Status::ExpAssignItem;
}
else
if ((status = translator.getToken(nextToken)) == Status::Good)
{
status = Status::ExpCmd;
if (nextToken->isCode(Comma_Code) || nextToken->isCode(Eq_Code))
{
return Status::ExpAssignItem;
}
}
return status;
return Status::ExpCmdOrAssignItem;
}

// get and check next token for comma or equal
Expand Down
3 changes: 2 additions & 1 deletion ibcp.h
Expand Up @@ -36,7 +36,7 @@ enum class Status
{
Good,
Done,
ExpCmd,
ExpCmdOrAssignItem,
ExpExpr,
ExpExprOrEnd,
ExpOpOrEnd,
Expand Down Expand Up @@ -71,6 +71,7 @@ enum class Status
ExpNonZeroDigit,
ExpDigitsOrSngDP,
ExpManDigits,
ExpExpSignOrDigits,
ExpExpDigits,
ExpDigits,
FPOutOfRange,
Expand Down
10 changes: 7 additions & 3 deletions parser.cpp
Expand Up @@ -243,7 +243,8 @@ TokenUniquePtr Parser::getNumber()
{
bool digits {}; // digits were found flag
bool decimal {}; // decimal point was found flag
bool sign {}; // have negative sign flag (2011-03-27)
bool sign {}; // have negative sign flag
bool expSign {}; // have exponent sign flag

int pos {m_pos};
forever
Expand Down Expand Up @@ -298,11 +299,12 @@ TokenUniquePtr Parser::getNumber()
}
// if there were no digits before 'E' then error
// (only would happen if mantissa contains only '.')
throw Error {Status::ExpManDigits, m_pos, 1};
throw Error {Status::ExpManDigits, m_pos, 2};
}
pos++; // move past 'e' or 'E'
if (m_input[pos] == '+' || m_input[pos] == '-')
{
expSign = true;
pos++; // move past exponent sign
}
// now look for exponent digits
Expand All @@ -314,7 +316,9 @@ TokenUniquePtr Parser::getNumber()
}
if (!digits) // no exponent digits found?
{
throw Error {Status::ExpExpDigits, pos, 1};
throw Error {expSign
? Status::ExpExpDigits : Status::ExpExpSignOrDigits, pos,
1};
}
decimal = true; // process as double
break; // exit loop to process string
Expand Down
8 changes: 5 additions & 3 deletions statusmessage.cpp
Expand Up @@ -34,8 +34,8 @@ const QString StatusMessage::text(Status status)
return tr("Good_TokenStatus (BUG)");
case Status::Done:
return tr("Done_TokenStatus (BUG)");
case Status::ExpCmd:
return tr("expected command");
case Status::ExpCmdOrAssignItem:
return tr("expected command or item for assignment");
case Status::ExpExpr:
return tr("expected expression");
case Status::ExpExprOrEnd:
Expand Down Expand Up @@ -105,9 +105,11 @@ const QString StatusMessage::text(Status status)
"constant");
case Status::ExpManDigits:
return tr("expected digits in mantissa of floating point constant");
case Status::ExpExpDigits:
case Status::ExpExpSignOrDigits:
return tr("expected sign or digits for exponent in floating point "
"constant");
case Status::ExpExpDigits:
return tr("expected digits for exponent in floating point constant");
case Status::ExpDigits:
return tr("expected digits in floating point constant");
case Status::FPOutOfRange:
Expand Down
6 changes: 3 additions & 3 deletions test/parser3.txt
Expand Up @@ -9,7 +9,7 @@ Input: .A
^-- expected digits in floating point constant

Input: .e
^-- expected digits in mantissa of floating point constant
^^-- expected digits in mantissa of floating point constant

Input: ..01
^^-- expected digits or single decimal point in floating point constant
Expand All @@ -36,10 +36,10 @@ Input: 100er
^-- expected sign or digits for exponent in floating point constant

Input: 100E+r
^-- expected sign or digits for exponent in floating point constant
^-- expected digits for exponent in floating point constant

Input: 100E-r
^-- expected sign or digits for exponent in floating point constant
^-- expected digits for exponent in floating point constant

Input: 100+200
0: Constant Integer 100 |100|
Expand Down
4 changes: 2 additions & 2 deletions test/translator01.out
Expand Up @@ -66,13 +66,13 @@ Input: A,B+C
^-- expected equal or comma for assignment

Input: 3=A
^-- expected item for assignment
^-- expected command or item for assignment

Input: A,3,B=4
^-- expected double variable

Input: (A=B)
^-- expected command
^-- expected command or item for assignment

Input: A,B,(C)=4
^-- expected double variable
Expand Down
4 changes: 2 additions & 2 deletions test/translator01.txt
Expand Up @@ -66,13 +66,13 @@ Input: A,B+C
^-- expected equal or comma for assignment

Input: 3=A
^-- expected item for assignment
^-- expected command or item for assignment

Input: A,3,B=4
^-- expected double variable

Input: (A=B)
^-- expected command
^-- expected command or item for assignment

Input: A,B,(C)=4
^-- expected double variable
Expand Down
2 changes: 1 addition & 1 deletion test/translator03.out
Expand Up @@ -69,7 +69,7 @@ Input: A,B%,1 = 1
^^-- expected double variable

Input: 1,A,B% = 1
^-- expected item for assignment
^-- expected command or item for assignment

Input: A%,B,C% = 1
^-- expected integer variable
Expand Down
2 changes: 1 addition & 1 deletion test/translator03.txt
Expand Up @@ -69,7 +69,7 @@ Input: A,B%,1 = 1
^^-- expected double variable

Input: 1,A,B% = 1
^-- expected item for assignment
^-- expected command or item for assignment

Input: A%,B,C% = 1
^-- expected integer variable
Expand Down
16 changes: 8 additions & 8 deletions test/translator07.out
Expand Up @@ -48,10 +48,10 @@ Input: Z+A
^-- expected equal or comma for assignment

Input: +Z
^-- expected command
^-- expected command or item for assignment

Input: -Z
^-- expected command
^-- expected command or item for assignment

Input: Z Y = 1
^-- expected equal or comma for assignment
Expand All @@ -63,7 +63,7 @@ Input: Z
^-- expected equal or comma for assignment

Input: ,Z
^-- expected command
^-- expected command or item for assignment

Input: Z,
^-- expected double variable
Expand Down Expand Up @@ -195,16 +195,16 @@ Input: MID$(A$,B,C)=
^-- expected string expression

Input: INT(
^^^^-- expected command
^^^^-- expected command or item for assignment

Input: INT(A,
^^^^-- expected command
^^^^-- expected command or item for assignment

Input: INT(A)
^^^^-- expected command
^^^^-- expected command or item for assignment

Input: INT(A)=
^^^^-- expected command
^^^^-- expected command or item for assignment

Input: LET INT(A)=
^^^^-- expected item for assignment
Expand All @@ -216,7 +216,7 @@ Input: A=INT(A)=
^-- expected numeric expression

Input: INT(A)=B
^^^^-- expected command
^^^^-- expected command or item for assignment

Input: LET ,
^-- expected item for assignment
Expand Down
16 changes: 8 additions & 8 deletions test/translator07.txt
Expand Up @@ -48,10 +48,10 @@ Input: Z+A
^-- expected equal or comma for assignment

Input: +Z
^-- expected command
^-- expected command or item for assignment

Input: -Z
^-- expected command
^-- expected command or item for assignment

Input: Z Y = 1
^-- expected equal or comma for assignment
Expand All @@ -63,7 +63,7 @@ Input: Z
^-- expected equal or comma for assignment

Input: ,Z
^-- expected command
^-- expected command or item for assignment

Input: Z,
^-- expected double variable
Expand Down Expand Up @@ -195,16 +195,16 @@ Input: MID$(A$,B,C)=
^-- expected string expression

Input: INT(
^^^^-- expected command
^^^^-- expected command or item for assignment

Input: INT(A,
^^^^-- expected command
^^^^-- expected command or item for assignment

Input: INT(A)
^^^^-- expected command
^^^^-- expected command or item for assignment

Input: INT(A)=
^^^^-- expected command
^^^^-- expected command or item for assignment

Input: LET INT(A)=
^^^^-- expected item for assignment
Expand All @@ -216,7 +216,7 @@ Input: A=INT(A)=
^-- expected numeric expression

Input: INT(A)=B
^^^^-- expected command
^^^^-- expected command or item for assignment

Input: LET ,
^-- expected item for assignment
Expand Down
6 changes: 3 additions & 3 deletions test/translator08.out
Expand Up @@ -78,13 +78,13 @@ Input: Z$(A%),Y$(A%)=X$(A%)
Output: Z$(A%), Y$(A%) = X$(A%)

Input: TAB(10)=A
^^^^-- expected command
^^^^-- expected command or item for assignment

Input: +
^-- expected command
^-- expected command or item for assignment

Input: NOT
^^^-- expected command
^^^-- expected command or item for assignment

Input: A PRINT B
^^^^^-- expected equal or comma for assignment
Expand Down
6 changes: 3 additions & 3 deletions test/translator08.txt
Expand Up @@ -78,13 +78,13 @@ Input: Z$(A%),Y$(A%)=X$(A%)
Output: A% Z$(<ref> A% Y$(<ref> A% X$( AssignList$

Input: TAB(10)=A
^^^^-- expected command
^^^^-- expected command or item for assignment

Input: +
^-- expected command
^-- expected command or item for assignment

Input: NOT
^^^-- expected command
^^^-- expected command or item for assignment

Input: A PRINT B
^^^^^-- expected equal or comma for assignment
Expand Down
2 changes: 1 addition & 1 deletion test/translator09.out
Expand Up @@ -51,7 +51,7 @@ Input: Z;
^-- expected equal or comma for assignment

Input: ;Z
^-- expected command
^-- expected command or item for assignment

Input: Z,;
^-- expected double variable
Expand Down
2 changes: 1 addition & 1 deletion test/translator09.txt
Expand Up @@ -51,7 +51,7 @@ Input: Z;
^-- expected equal or comma for assignment

Input: ;Z
^-- expected command
^-- expected command or item for assignment

Input: Z,;
^-- expected double variable
Expand Down
11 changes: 9 additions & 2 deletions test/translator14.dat
Expand Up @@ -9,8 +9,9 @@
# 3: expected digits or single decimal point in floating point constant
# 4: expected digits in mantissa in floating point constant
# 5: expected sign or digits for exponent in floating point constant
# 6: expected digits in floating point constan
# 7: floating point constant is out of range
# 6: expected digits for exponent in floating point constant
# 7: expected digits in floating point constant
# 8: floating point constant is out of range

?
01
Expand Down Expand Up @@ -62,6 +63,7 @@ MID$(A$.e

MID$(A$,?
MID$(A$,1e
MID$(A$,1e+

MID$(A$,3?
MID$(A$,3 .
Expand All @@ -80,6 +82,7 @@ A$=MID$(B$.e

A$=MID$(B$,?
A$=MID$(B$,1e
A$=MID$(B$,1e-

A$=MID$(B$,3?
A$=MID$(B$,3 .
Expand All @@ -98,9 +101,11 @@ PRINT A.e

PRINT (?
PRINT (1e
PRINT (1e-

PRINT A(?
PRINT A(1e
PRINT A(1e+
PRINT F(?

PRINT A(1?
Expand All @@ -118,12 +123,14 @@ PRINT A,,..

PRINT INT(?
PRINT INT(1e
PRINT INT(1e-

PRINT ASC(A$?
PRINT ASC(A$1e

PRINT ASC(A$,?
PRINT ASC(A$,1e
PRINT ASC(A$,1e+

PRINT TAB(?
PRINT SPC(..
Expand Down

0 comments on commit 20e46cc

Please sign in to comment.