Skip to content

Commit

Permalink
Improve error message for unit lexing failure
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoelund committed Jun 11, 2020
1 parent 1521bd2 commit 18937a4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
22 changes: 12 additions & 10 deletions OMCompiler/Compiler/BackEnd/Unit.mo
Expand Up @@ -563,6 +563,7 @@ end prefix2String;
public function parseUnitString "author: lochel
The second argument is optional."
input String inUnitString;
input SourceInfo info;
input HashTableStringToUnit.HashTable inKnownUnits = getKnownUnits();
output Unit outUnit;
protected
Expand All @@ -573,7 +574,7 @@ algorithm
if listEmpty(charList) then
fail();
end if;
tokenList := lexer(charList);
tokenList := lexer(charList, info);
outUnit := parser3({true, true}, tokenList, UNIT(1e0, 0, 0, 0, 0, 0, 0, 0), inKnownUnits);
if not isUnit(outUnit) then
fail();
Expand Down Expand Up @@ -809,6 +810,7 @@ end getPrefix;
protected function lexer "author: lochel
Tokenizer: charList to tokenList"
input list<String> inCharList;
input SourceInfo info;
output list<Token> outTokenList;
algorithm
outTokenList := matchcontinue(inCharList)
Expand All @@ -822,50 +824,50 @@ algorithm
case {} then {};

case "."::charList equation
tokenList = lexer(charList);
tokenList = lexer(charList, info);
then T_MUL()::tokenList;

case "("::charList equation
tokenList = lexer(charList);
tokenList = lexer(charList, info);
then T_LPAREN()::tokenList;

case ")"::charList equation
tokenList = lexer(charList);
tokenList = lexer(charList, info);
then T_RPAREN()::tokenList;

case "/"::charList equation
tokenList = lexer(charList);
tokenList = lexer(charList, info);
then T_DIV()::tokenList;

case "+"::charList equation
(charList, number) = popNumber(charList);
false = (number == "");
tokenList = lexer(charList);
tokenList = lexer(charList, info);
i = stringInt(number);
then T_NUMBER(i)::tokenList;

case "-"::charList equation
(charList, number) = popNumber(charList);
false = (number == "");
tokenList = lexer(charList);
tokenList = lexer(charList, info);
i = -stringInt(number);
then T_NUMBER(i)::tokenList;

case charList equation
(charList, number) = popNumber(charList);
false = (number == "");
tokenList = lexer(charList);
tokenList = lexer(charList, info);
i = stringInt(number);
then T_NUMBER(i)::tokenList;

case charList equation
(charList, unit) = popUnit(charList);
false = (unit == "");
tokenList = lexer(charList);
tokenList = lexer(charList, info);
then T_UNIT(unit)::tokenList;

else equation
Error.addInternalError("function lexer failed", sourceInfo());
Error.addInternalError("function lexer failed: " + stringDelimitList(list("'" + c + "'" for c in inCharList), " "), info);
then fail();
end matchcontinue;
end lexer;
Expand Down
5 changes: 3 additions & 2 deletions OMCompiler/Compiler/BackEnd/UnitCheck.mo
Expand Up @@ -995,7 +995,7 @@ algorithm
guard(unitString <> "")
equation
cr = BackendVariable.varCref(var);
(ut, HtS2U, HtU2S) = parse(unitString, cr, HtS2U, HtU2S);
(ut, HtS2U, HtU2S) = parse(unitString, cr, var.source.info, HtS2U, HtU2S);
HtCr2U = BaseHashTable.add((cr,ut),HtCr2U);
then ((HtCr2U, HtS2U, HtU2S));

Expand All @@ -1016,6 +1016,7 @@ end convertUnitString2unit;
protected function parse "author: lochel"
input String inUnitString;
input DAE.ComponentRef inCref;
input SourceInfo info;
input HashTableStringToUnit.HashTable inHtS2U;
input HashTableUnitToString.HashTable inHtU2S;
output Unit.Unit outUnit;
Expand All @@ -1030,7 +1031,7 @@ algorithm
then Unit.MASTER({inCref});

case _
then Unit.parseUnitString(inUnitString, inHtS2U);
then Unit.parseUnitString(inUnitString, info, inHtS2U);

else Unit.UNKNOWN(inUnitString);
end matchcontinue;
Expand Down
4 changes: 2 additions & 2 deletions OMCompiler/Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -8193,7 +8193,7 @@ algorithm
deriv.comment := "der(" + deriv.comment + ")";
end if;
try
unit := Unit.parseUnitString(deriv.unit);
unit := Unit.parseUnitString(deriv.unit, state.source.info);
unit := Unit.unitDiv(unit, Unit.UNIT(1e0, 0, 0, 0, 1, 0, 0, 0));
deriv.unit := Unit.unitString(unit);
else
Expand Down Expand Up @@ -9146,7 +9146,7 @@ algorithm
if not stringEq(var.unit, "") and not BaseHashSet.has(var.unit, unitNameKeys) then
unitNameKeys := BaseHashSet.add(var.unit, unitNameKeys);
try
unit := Unit.parseUnitString(var.unit); // get the SI- units information
unit := Unit.parseUnitString(var.unit, var.source.info); // get the SI- units information
unitDefinitions := SimCode.UNITDEFINITION(var.unit, transformUnitToBaseUnit(unit)) :: unitDefinitions;
else
// catch the units which are not calculated
Expand Down

0 comments on commit 18937a4

Please sign in to comment.