Skip to content

Commit

Permalink
Merge pull request #45 from FnControlOption/comma
Browse files Browse the repository at this point in the history
Allow trailing comma in enum
  • Loading branch information
sashi0034 authored Dec 25, 2024
2 parents 91579e8 + 10ebef5 commit ce66e5a
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 3 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Tests

on: [push]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'
- run: npm ci
- run: npm run compile
- run: npm test --prefix server
175 changes: 175 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
"@typescript-eslint/parser": "^7.1.0",
"eslint": "^8.57.0",
"mocha": "^10.3.0",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
}
}
5 changes: 5 additions & 0 deletions server/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extension": ["ts"],
"spec": "test/**/*.spec.ts",
"require": "ts-node/register"
}
4 changes: 3 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@
"vscode-languageserver": "^9.0.1",
"vscode-languageserver-textdocument": "^1.0.11"
},
"scripts": {}
"scripts": {
"test": "mocha"
}
}
9 changes: 7 additions & 2 deletions server/src/compile/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ function expectContextualKeyword(parser: ParserState, keyword: string): boolean
return true;
}

// ENUM ::= {'shared' | 'external'} 'enum' IDENTIFIER (';' | ('{' IDENTIFIER ['=' EXPR] {',' IDENTIFIER ['=' EXPR]} '}'))
// ENUM ::= {'shared' | 'external'} 'enum' IDENTIFIER (';' | ('{' IDENTIFIER ['=' EXPR] {',' IDENTIFIER ['=' EXPR]} [','] '}'))
function parseEnum(parser: ParserState): ParsedResult<NodeEnum> {
const rangeStart = parser.next();

Expand Down Expand Up @@ -254,13 +254,18 @@ function parseEnum(parser: ParserState): ParsedResult<NodeEnum> {
};
}

// '{' IDENTIFIER ['=' EXPR] {',' IDENTIFIER ['=' EXPR]} '}'
// '{' IDENTIFIER ['=' EXPR] {',' IDENTIFIER ['=' EXPR]} [','] '}'
function expectEnumMembers(parser: ParserState): ParsedEnumMember[] {
const members: ParsedEnumMember[] = [];
parser.expect('{', HighlightToken.Operator);
while (parser.isEnd() === false) {
if (expectContinuousOrClose(parser, ',', '}', members.length > 0) === BreakOrThrough.Break) break;

if (parser.next().text === '}') {
parser.commit(HighlightToken.Operator);
break;
}

const identifier = expectIdentifier(parser, HighlightToken.EnumMember);
if (identifier === undefined) break;

Expand Down
68 changes: 68 additions & 0 deletions server/test/compile/parser.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {tokenize} from "../../src/compile/tokenizer";
import {parseFromTokenized} from "../../src/compile/parser";
import {diagnostic} from '../../src/code/diagnostic';
import {preprocessTokensForParser} from "../../src/compile/parserPreprocess";

function itParses(content: string) {
it(`parses ${content}`, () => {
diagnostic.beginSession();

const targetUri = "/foo/bar.as";
const tokenizedTokens = tokenize(content, targetUri);
const preprocessedTokens = preprocessTokensForParser(tokenizedTokens);
parseFromTokenized(preprocessedTokens.parsingTokens);

const diagnosticsInAnalyzer = diagnostic.endSession();
if (diagnosticsInAnalyzer.length > 0) {
const diagnostic = diagnosticsInAnalyzer[0];
const message = diagnostic.message;
const line = diagnostic.range.start.line;
const character = diagnostic.range.start.character;
throw new Error(`${message} (:${line}:${character})`);
}
});
}

describe("Parser", () => {
itParses("void foo() {}");
itParses("int MyValue = 0;");
itParses("const uint Flag1 = 0x01;");
itParses(`
class Foo
{
void bar() { value++; }
int value;
}
`);
itParses(`
interface MyInterface
{
void DoSomething();
}
`);
itParses(`
enum MyEnum
{
eValue0,
eValue2 = 2,
eValue3,
eValue200 = eValue2 * 100
}
`);
itParses(`
enum Foo
{
fizz,
buzz,
}
`);
itParses("funcdef bool CALLBACK(int, int);");
itParses("typedef double real64;");
itParses(`
namespace A
{
void function() { variable++; }
int variable;
}
`);
});

0 comments on commit ce66e5a

Please sign in to comment.