Skip to content

Commit

Permalink
feat(core): improve a bit the parser
Browse files Browse the repository at this point in the history
In case a statment start with 'open' or 'connect' then it will be only parsed against, respectively, open or connect slangroom statements.
  • Loading branch information
matteo-cristino committed May 9, 2024
1 parent 2fd415a commit 3bbcfc5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/core/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,17 @@ export const parse = (p: PluginMap, t: Token[], lineNo: number): Cst => {
errors: [],
};
let givenThen: 'given' | 'then' | undefined;
let openConnect: 'open' | 'connect' | undefined;
if (t[0]) {
if (t[0].name != 'given' && t[0].name != 'then')
cst.errors.push({ message: ParseError.wrong(t[0], 'given', 'then'), lineNo, start: t[0].start, end: t[0].end });
else givenThen = t[0].name;
if (t[1]) {
if (t[1].raw !== 'I') cst.errors.push({message: ParseError.wrong(t[1], 'I'), lineNo, start: t[1].start, end: t[1].end});
if (t[2]) {
if (t[2].raw == 'open') openConnect = 'open';
else if (t[2].raw == 'connect') openConnect = 'connect';
}
} else cst.errors.push({ message: ParseError.missing(t[0], 'I'), lineNo, start: t[0].start, end: t[0].end});
} else {
cst.errors.push({ message: ParseError.missing(lineNo, 'Given I', 'Then I'), lineNo});
Expand All @@ -246,6 +251,9 @@ export const parse = (p: PluginMap, t: Token[], lineNo: number): Cst => {
if (curErrLen !== undefined && m.err.length > curErrLen) throw lemmeout;
};
try {
// check open and connect statement only against the correct statements
if(openConnect && (openConnect !== k.openconnect)) throw lemmeout;

// Open 'ident' and|Connect to 'ident' and
if (k.openconnect === 'open') {
if (t[++i]?.name !== 'open') newErr(i, 'open');
Expand Down
27 changes: 27 additions & 0 deletions pkg/core/test/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,33 @@ test('parser works', (t) => {
},
],
},
"Given I connect to": {
givenThen: 'given',
errors: [],
matches: [
{
key: {
openconnect: 'connect',
phrase: 'send http request',
params: ['object'],
},
bindings: new Map(),
err: [
{message: ParseError.missing(new Token('to', 1, 16, 17), '\'<identifier>\''), lineNo: 1},
{message: ParseError.missing(1, 'and'), lineNo: 1},
{message: ParseError.missing(1, 'send'), lineNo: 1},
{message: ParseError.missing(1, 'object'), lineNo: 1},
{message: ParseError.missing(1, '\'<identifier>\''), lineNo: 1},
{message: ParseError.missing(1, 'and'), lineNo: 1},
{message: ParseError.missing(1, 'send'), lineNo: 1},
{message: ParseError.missing(1, 'http'), lineNo: 1},
{message: ParseError.missing(1, 'request'), lineNo: 1},

],
lineNo: 1,
},
],
},
}).forEach(([give, want], index) => {
const lexed = lex(give, 1);
if (!lexed.ok) throw new Error(lexed.error.message.message);
Expand Down

0 comments on commit 3bbcfc5

Please sign in to comment.