Skip to content
Snippets Groups Projects
Commit 048d2784 authored by Sebastian Biewer's avatar Sebastian Biewer
Browse files

Using Peggy's native `error` function to throw for syntax errors not captured by the grammar

parent a0e62c1a
Branches
Tags
No related merge requests found
Pipeline #45734 passed
......@@ -7,7 +7,7 @@
- Updated parser generator to Peggy 2.0.1
- Exporting PeggySyntaxError
- Added `options` argument to `parse` function
- Added a new class PseucoSyntaxError for errors thrown during parsing, but noy by the Peggy framework, but by us
- Using Peggy's native `error` function to throw for syntax errors not captured by the grammar
- Renamed "Parser.ts", respectively, "dist/Parser.js" into "parser-generated.ts" ("parser-generated.js") to make clear that these files are generated
## 1.0
......
......@@ -279,8 +279,6 @@ Digit
Program
= source:SourceElements
{
//source.unshift(location().start.line, location().start.column);
//return construct(PCProgram, source);
return new Program(location().start.line, location().start.column, ...source)
}
......@@ -321,8 +319,6 @@ SourceElement
Monitor
= "monitor" ___ id:Identifier __ "{" __ code:ClassCode __ "}"
{
//code.unshift(id, location().start.line, location().start.column);
//return construct(PCMonitor, code);
return new Monitor(location().start.line, location().start.column, id, ...code)
}
......@@ -334,8 +330,6 @@ Monitor
Struct
= "struct" ___ id:Identifier __ "{" __ code:ClassCode "}"
{
//code.unshift(id, location().start.line, location().start.column);
//return construct(PCStruct, code);
return new Struct(location().start.line, location().start.column, id, ...code)
}
......@@ -381,9 +375,6 @@ Procedure
= type:ResultType ___ id:Identifier __ fp:FormalParameters __
stmtBlock:StatementBlock
{
// fp.unshift(location().start.line, location().start.column, type, id,
// stmtBlock);
// return construct(PCProcedureDecl, fp);
return new ProcedureDeclaration(location().start.line, location().start.column, type, id, fp, stmtBlock)
}
......@@ -448,21 +439,11 @@ Statement
}
/ "join" ___ exp:Expression __ ";"
{
throw new PseucoSyntaxError(
location().start.line,
location().start.column,
"OldSyntax",
old_syntax
);
error(old_syntax);
}
/ "unlock" ___ exp:Expression __ ";"
{
throw new PseucoSyntaxError(
location().start.line,
location().start.column,
"OldSyntax",
old_syntax
);
error(old_syntax);
}
/ stmt:Println
{
......@@ -532,12 +513,12 @@ Statement
StatementBlock
= "{" __ blockStmts:(BlockStatement __)* "}"
{
var stmts = []//[location().start.line, location().start.column];
var stmts = []
for (var i = 0; i < blockStmts.length; ++i)
{
stmts.push(blockStmts[i][0]);
}
//return construct(PCStmtBlock, stmts);
return new StatementBlock(location().start.line, location().start.column, ...stmts)
}
......@@ -550,10 +531,9 @@ BlockStatement
return new StatementWrapper(location().start.line, location().start.column, stmt);
}
/ stmt:Procedure {
throw new PseucoSyntaxError(location().start.line, location().start.column, "Nested Procedure", "Nesting procedures is not possible! You can define procedures globally, in monitors or structs.");
error("Nesting procedures is not possible! You can define procedures globally, in monitors or structs.");
}
/ stmt:DeclarationStatement {
//return new StatementWrapper(location().start.line, location().start.column, stmt);
return stmt;
}
/ stmt:ConditionDeclarationStatement { return stmt; }
......@@ -589,15 +569,12 @@ DeclarationStatement
Declaration
= type:Type ___ head:VariableDeclarator tail:(__ "," __ VariableDeclarator)*
{
// var declarations = [];
// declarations.push(false, location().start.line,
// location().start.column, type, head);
const declarations = [head];
for (var i = 0; i < tail.length; ++i)
{
declarations.push(tail[i][3]);
}
//return construct(PCDecl, declarations);
return new VariableDeclarations(location().start.line, location().start.column, type, ...declarations)
}
......@@ -607,11 +584,6 @@ Declaration
VariableDeclarator
= id:Identifier varInit:(__ "=" __ VariableInitializer)?
{
// return varInit != null ?
// new PCVariableDeclarator(location().start.line,
// location().start.column, id, varInit[3]) :
// new PCVariableDeclarator(location().start.line,
// location().start.column, id);
if (varInit) {
return new VariableDeclarator(location().start.line, location().start.column, id, varInit[3]);
} else {
......@@ -635,22 +607,16 @@ VariableInitializer
{
inits.push(test[1][i][3]);
}
// inits.unshift(location().start.line, location().start.column,
// uncomplete != null);
// return construct(PCVariableInitializer, inits);
return new VariableInitializer(location().start.line, location().start.column, uncomplete != null, ...inits);
}
else
{
// return new PCVariableInitializer(location().start.line,
// location().start.column, uncomplete != null);
return new VariableInitializer(location().start.line, location().start.column, uncomplete != null);
}
}
/ exp:Expression
{
// return new PCVariableInitializer(location().start.line,
// location().start.column, false, exp);
return new VariableInitializer(location().start.line, location().start.column, false, exp);
}
......@@ -735,12 +701,12 @@ StatementExpressionList
SelectStatement
= "select" __ "{" __ stmts:(CaseStatement __ )+ __ "}"
{
var caseStmts = []; //[location().start.line, location().start.column];
var caseStmts = [];
for (var i = 0; i < stmts.length; ++i)
{
caseStmts.push(stmts[i][0]);
}
//return construct(PCSelectStmt, caseStmts);
return new SelectStatement(location().start.line, location().start.column, ...caseStmts);
}
......@@ -763,7 +729,7 @@ CaseStatement
stmt);
}
/ "case" ___ exp:StatementExpression __ ":" __ stmt:Statement {
throw new PseucoSyntaxError(location().start.line, location().start.column, "Old Syntax", "You are using old pseuCo syntax. Case conditions must be send (chan <! val) or receive (<? chan) expressions or assignments of received values (x = <? chan).");
error("You are using old pseuCo syntax. Case conditions must be send (chan <! val) or receive (<? chan) expressions or assignments of received values (x = <? chan).");
}
/*
......@@ -829,9 +795,7 @@ ForStatement
{
res = res.concat(update);
}
// res.unshift(location().start.line, location().start.column, stmt,
// init, exp);
// return construct(PCForStmt, res);
return new ForStatement(location().start.line, location().start.column, stmt, init, exp, ...res);
}
......@@ -843,13 +807,12 @@ ForInit
= head:(Declaration / StatementExpression) tail:(__ "," __ (Declaration /
StatementExpression))*
{
var inits = [head]; // [location().start.line, location().start.column];
//inits.push(head);
var inits = [head];
for (var i = 0; i < tail.length; ++i)
{
inits.push(tail[i][3]);
}
//return construct(PCForInit, inits);
return new ForInit(location().start.line, location().start.column, ...inits);
}
......@@ -883,50 +846,30 @@ ReturnStatement
PrimitiveStatement
= "join" __ "(" __ exp:Expression __ ")" __ ";"
{
// return new PCPrimitiveStmt(location().start.line,
// location().start.column, PCPrimitiveStmt.JOIN, exp);
return new PrimitiveStatement(location().start.line, location().start.column, "join", exp);
}
/ "lock" __ "(" __ exp:Expression __ ")" __ ";"
{
// return new PCPrimitiveStmt(location().start.line,
// location().start.column, PCPrimitiveStmt.LOCK, exp);
return new PrimitiveStatement(location().start.line, location().start.column, "lock", exp);
}
/ "unlock" __ "(" __ exp:Expression __ ")" __ ";"
{
return new PrimitiveStatement(location().start.line, location().start.column, "unlock", exp);
// return new PCPrimitiveStmt(location().start.line,
// location().start.column, PCPrimitiveStmt.UNLOCK, exp);
}
/ "waitForCondition" helper:ConditionHelper
{
if (helper == null) {
throw new PseucoSyntaxError(
location().start.line,
location().start.column,
"Syntax Error",
"Expected a condition object to wait for!"
);
error("Expected a condition object to wait for!");
} else {
return new ConditionStatement(location().start.line, location().start.column, "waitForCondition", helper);
// return new PCPrimitiveStmt(location().start.line,
// location().start.column, PCPrimitiveStmt.WAIT, exp);
}
}
/ "signal" helper:ConditionHelper
{
if (helper == null) {
throw new PseucoSyntaxError(
location().start.line,
location().start.column,
"Syntax Error",
"Expected a condition object to signal!"
);
error("Expected a condition object to signal!");
} else {
return new ConditionStatement(location().start.line, location().start.column, "signal", helper);
// return new PCPrimitiveStmt(location().start.line,
// location().start.column, PCPrimitiveStmt.SIGNAL, exp);
}
}
/ "signalAll" helper:ConditionHelper
......@@ -936,19 +879,10 @@ PrimitiveStatement
} else {
return new ConditionStatement(location().start.line, location().start.column, "signalAll", helper);
}
// if (helper == null) {
// return new PCPrimitiveStmt(location().start.line,
// location().start.column, PCPrimitiveStmt.SIGNAL_ALL);
// } else {
// return new PCPrimitiveStmt(location().start.line,
// location().start.column, PCPrimitiveStmt.SIGNAL_ALL, helper);
// }
}
/ "assert" __ "(" __ exp:Expression __ ")" __ ";"
{
return new PrimitiveStatement(location().start.line, location().start.column, "assert", exp);
// return new PCPrimitiveStmt(location().start.line,
// location().start.column, PCPrimitiveStmt.ASSERT, exp);
}
......@@ -968,30 +902,15 @@ ConditionHelper
}
/ __ "(" __ Expression __ ")" __ ";"
{
throw new PseucoSyntaxError(
location().start.line,
location().start.column,
"Syntax Error",
"Expected an identifier representing a condition object!"
);
error("Expected an identifier representing a condition object!");
}
/ __ "(" __ Expression (__ "," __ Expression)* __ ")" __ ";"
{
throw new PseucoSyntaxError(
location().start.line,
location().start.column,
"Syntax Error",
"Expected a single identifier representing a condition object!"
);
error("Expected a single identifier representing a condition object!");
}
/ ___ exp:Expression __ ";"
{
throw new PseucoSyntaxError(
location().start.line,
location().start.column,
"OldSyntax",
old_syntax
);
error(old_syntax);
}
......@@ -1002,8 +921,6 @@ ConditionHelper
Println
= "println" __ "(" __ expList:ExpressionList __ ")" __ ";"
{
// expList.unshift(location().start.line, location().start.column);
// return construct(PCPrintStmt, expList);
return new PrintStatement(location().start.line, location().start.column, ...expList);
}
......@@ -1023,49 +940,30 @@ PrimitiveType
/ "bool"
{
return new SimpleTypeNode(location().start.line, location().start.column, "bool");
// return new PCSimpleType(location().start.line,
// location().start.column, PCSimpleType.BOOL);
}
/ "int"
{
return new SimpleTypeNode(location().start.line, location().start.column, "int");
// return new PCSimpleType(location().start.line,
// location().start.column, PCSimpleType.INT);
}
/ "string"
{
return new SimpleTypeNode(location().start.line, location().start.column, "string");
// return new PCSimpleType(location().start.line,
// location().start.column, PCSimpleType.STRING);
}
/ "lock"
{
return new SimpleTypeNode(location().start.line, location().start.column, "lock");
// return new PCSimpleType(location().start.line,
// location().start.column, PCSimpleType.LOCK);
}
/ "mutex"
{
// return new PCSimpleType(location().start.line,
// location().start.column, PCSimpleType.MUTEX);
throw new PseucoSyntaxError(
location().start.line,
location().start.column,
"MutexDeprecated",
"Type 'mutex' is deprecated. Use 'lock' instead."
);
error("Type 'mutex' is deprecated. Use 'lock' instead.");
}
/ "agent"
{
return new SimpleTypeNode(location().start.line, location().start.column, "agent");
// return new PCSimpleType(location().start.line,
// location().start.column, PCSimpleType.AGENT);
}
/ id:Identifier
{
return new ClassTypeNode(location().start.line, location().start.column, id);
// return new PCClassType(location().start.line,
// location().start.column, id);
}
/*
......@@ -1075,23 +973,14 @@ Chan
= "intchan" int_:(IntegerLiteral)?
{
return new ChannelTypeNode(location().start.line, location().start.column, "intchan", int_); // Constructor can handle int_ == null
// return new PCChannelType(location().start.line,
// location().start.column, PCSimpleType.INT,
// int_ != null ? int_ : PCChannelType.CAPACITY_UNKNOWN);
}
/ "boolchan" int_:(IntegerLiteral)?
{
return new ChannelTypeNode(location().start.line, location().start.column, "boolchan", int_); // Constructor can handle int_ == null
// return new PCChannelType(location().start.line,
// location().start.column, PCSimpleType.BOOL,
// int_ != null ? int_ : PCChannelType.CAPACITY_UNKNOWN);
}
/ "stringchan" int_:(IntegerLiteral)?
{
return new ChannelTypeNode(location().start.line, location().start.column, "stringchan", int_); // Constructor can handle int_ == null
// return new PCChannelType(location().start.line,
// location().start.column, PCSimpleType.STRING,
// int_ != null ? int_ : PCChannelType.CAPACITY_UNKNOWN);
}
/*
......@@ -1103,8 +992,6 @@ ResultType
= "void"
{
return new SimpleTypeNode(location().start.line, location().start.column, "void");
// return new PCSimpleType(location().start.line,
// location().start.column, PCSimpleType.VOID);
}
/ type:Type { return type; }
......@@ -1128,17 +1015,10 @@ StartExpression
= "start" __ "(" __ exp:(MonCall / ProcCall) __ ")" __
{
return new StartExpression(location().start.line, location().start.column, exp);
// return new PCStartExpression(location().start.line,
// location().start.column, exp);
}
/ "start" ___ exp:(MonCall / ProcCall) __
{
throw new PseucoSyntaxError(
location().start.line,
location().start.column,
"OldSyntax",
old_syntax
);
error(old_syntax);
}
/*
......@@ -1163,8 +1043,6 @@ AssignmentExpression
= dest:AssignDestination __ op:AssignmentOperator __ exp:Expression
{
return new AssignExpression(location().start.line, location().start.column, dest, op, exp);
// return new PCAssignExpression(location().start.line,
// location().start.column, dest, op, exp);
}
/*
......@@ -1179,8 +1057,7 @@ AssignDestination
{
index.push(pos[i][2]);
}
// index.unshift(id, location().start.line, location().start.column);
// return construct(PCAssignDestination, index);
return new AssignDestination(location().start.line, location().start.column, id, ...index);
}
......@@ -1207,8 +1084,6 @@ SendExpression
= callExp:CallExpression __ "<!" __ exp:Expression __
{
return new SendExpression(location().start.line, location().start.column, callExp, exp);
// return new PCSendExpression(location().start.line,
// location().start.column, callExp, exp);
}
/*
......@@ -1222,10 +1097,6 @@ ConditionalExpression
= exp:ConditionalOrExpression rest:(__ "?" __ Expression __ ":" __
ConditionalExpression)?
{
// return rest != null ?
// new PCConditionalExpression(location().start.line,
// location().start.column, exp, rest[3], rest[7]) :
// exp;
if (rest != null) {
return new ConditionalExpression(location().start.line, location().start.column, exp, rest[3], rest[7]);
} else {
......@@ -1360,7 +1231,6 @@ UnaryExpression
return new UnaryExpression(location().start.line, location().start.column, op, exp);
}
/ exp:ReceiveExpression { return exp; }
/// exp:PostfixExpression { return exp; }
/*
* Increment operation: expression++
......@@ -1413,8 +1283,6 @@ CallExpression
ProcCall
= id:Identifier __ args:Arguments
{
// args.unshift(id, location().start.line, location().start.column);
// return construct(PCProcedureCall, args);
return new ProcedureCall(location().start.line, location().start.column, id, ...args);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment