Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[memory][scs][tools][server] Implement SCs AST parse listener #280

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
[memory][scs] Fix token recognition error position
NikitaZotov committed Jun 17, 2023
commit 9b1e5fa72a306c2fc9151980a6b09d09739f559b
16 changes: 6 additions & 10 deletions sc-memory/sc-memory/scs/ASTJsonListener.cpp
Original file line number Diff line number Diff line change
@@ -11,19 +11,15 @@ void ASTErrorListener::syntaxError(
std::exception_ptr)
{
ScJson errorInfoJson;
errorInfoJson["token"] = token ? token->getText() : "Invalid token";
errorInfoJson["line"] = line;
errorInfoJson["charPositionInLine"] = charPositionInLine;
errorInfoJson["token"] = token ? ScJson(token->getText()) : ScJson();
errorInfoJson["msg"] = msg;

ScJson positionJson;
if (token != nullptr)
{
positionJson["beginLine"] = token->getLine();
positionJson["beginIndex"] = charPositionInLine;
positionJson["endLine"] = token->getLine();
positionJson["endIndex"] = token->getText().size() + charPositionInLine;
}
positionJson["beginLine"] = token == nullptr ? line : token->getLine();
positionJson["beginIndex"] = charPositionInLine;
positionJson["endLine"] = token == nullptr ? positionJson["beginLine"].get<size_t>() : token->getLine();
positionJson["endIndex"]
= token == nullptr ? positionJson["beginIndex"].get<size_t>() : token->getText().size() + charPositionInLine;

errorInfoJson["position"] = positionJson;

47 changes: 34 additions & 13 deletions sc-memory/tests/scs/units/test_ast.cpp
Original file line number Diff line number Diff line change
@@ -402,8 +402,6 @@ TEST(scs_ast, IncorrectAST1)

EXPECT_EQ(R"({
"errors": [{
"charPositionInLine": 1,
"line": 1,
"msg": "no viable alternative at input 'x'",
"position": {
"beginIndex": 1,
@@ -468,14 +466,16 @@ TEST(scs_ast, IncorrectAST2)

EXPECT_EQ(R"({
"errors": [{
"charPositionInLine": 22,
"line": 1,
"msg": "token recognition error at: ']'",
"position": null,
"token": "Invalid token"
"position": {
"beginIndex": 22,
"beginLine": 1,
"endIndex": 22,
"endLine": 1
},
"token": null
}, {
"charPositionInLine": 23,
"line": 1,
"msg": "missing ';;' at '<EOF>'",
"position": {
"beginIndex": 23,
@@ -623,18 +623,39 @@ TEST(scs_ast, IncorrectAST2)

TEST(scs_ast, IncorrectAST3)
{
sc_char const * data = "x => nrel_idtf: adsa;;ж";
sc_char const * data = "x => nrel_idtf: adsa;;жоо";

scs::Parser parser;
std::string const & ast = parser.BuildAST(data);

EXPECT_EQ(R"({
"errors": [{
"charPositionInLine": 22,
"line": 1,
"msg": "token recognition error at: 'ж'",
"position": null,
"token": "Invalid token"
"position": {
"beginIndex": 22,
"beginLine": 1,
"endIndex": 22,
"endLine": 1
},
"token": null
}, {
"msg": "token recognition error at: 'о'",
"position": {
"beginIndex": 23,
"beginLine": 1,
"endIndex": 23,
"endLine": 1
},
"token": null
}, {
"msg": "token recognition error at: 'о'",
"position": {
"beginIndex": 24,
"beginLine": 1,
"endIndex": 24,
"endLine": 1
},
"token": null
}],
"root": {
"children": [{
@@ -773,7 +794,7 @@ TEST(scs_ast, IncorrectAST3)
"position": {
"beginIndex": 0,
"beginLine": 1,
"endIndex": 28,
"endIndex": 30,
"endLine": 1
},
"ruleType": "syntax",