From 6d8e76810983fe4de03c66933317ac3dadf6bd62 Mon Sep 17 00:00:00 2001 From: Forest Belton Date: Mon, 13 Sep 2021 18:20:12 -0400 Subject: [PATCH] Begin building parser --- .gitignore | 1 + Makefile | 11 +++++++---- package-lock.json | 23 +++++++++++++++++++++++ package.json | 17 +++++++++++++++-- parser.pegjs | 27 +++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 parser.pegjs diff --git a/.gitignore b/.gitignore index 5244e66..dd27862 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ gbuoy.js +parser.js diff --git a/Makefile b/Makefile index ee00a98..90e2272 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,7 @@ -TSFILES := index.ts $(find lib -type f -name '*.ts') - -gbuoy.js: $(TSFILES) - ./node_modules/.bin/tsc --outFile $@ $(TSFILES) +TSFILES := index.ts $(find lib -type f -name '*.ts') + +gbuoy.js: parser.js $(TSFILES) + ./node_modules/.bin/tsc --outFile $@ $(TSFILES) + +parser.js: parser.pegjs + ./node_modules/.bin/peggy -o $@ $< diff --git a/package-lock.json b/package-lock.json index 0df9e5d..f8d0f23 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,13 +1,30 @@ { "name": "gbuoy", + "version": "1.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { + "name": "gbuoy", + "version": "1.0.0", + "license": "MIT", "devDependencies": { + "peggy": "^1.2.0", "typescript": "^4.4.3" } }, + "node_modules/peggy": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/peggy/-/peggy-1.2.0.tgz", + "integrity": "sha512-PQ+NKpAobImfMprYQtc4Egmyi29bidRGEX0kKjCU5uuW09s0Cthwqhfy7mLkwcB4VcgacE5L/ZjruD/kOPCUUw==", + "dev": true, + "bin": { + "peggy": "bin/peggy" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/typescript": { "version": "4.4.3", "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.3.tgz", @@ -23,6 +40,12 @@ } }, "dependencies": { + "peggy": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/peggy/-/peggy-1.2.0.tgz", + "integrity": "sha512-PQ+NKpAobImfMprYQtc4Egmyi29bidRGEX0kKjCU5uuW09s0Cthwqhfy7mLkwcB4VcgacE5L/ZjruD/kOPCUUw==", + "dev": true + }, "typescript": { "version": "4.4.3", "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.3.tgz", diff --git a/package.json b/package.json index cf1fe7d..e5bf7cd 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,18 @@ { "devDependencies": { + "peggy": "^1.2.0", "typescript": "^4.4.3" - } -} + }, + "name": "gbuoy", + "version": "1.0.0", + "main": "gbuoy.js", + "directories": { + "lib": "lib" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Forest Belton ", + "license": "MIT", + "description": "A 'high-level' language for the Gameboy" +} \ No newline at end of file diff --git a/parser.pegjs b/parser.pegjs new file mode 100644 index 0000000..3964ab3 --- /dev/null +++ b/parser.pegjs @@ -0,0 +1,27 @@ +// AST factories +{ + const attr = (name, args) => ({ name, args }) +} + +// Attributes +Attrs = LATTR l:AttrList RATTR { return l } +AttrList = h:Attr t:(COMMA @Attr)* { return [h].concat(t) } + / '' { return [] } +Attr = name:Ident args:(LPAREN @AttrArgs RPAREN)? { return attr(name, args || []) } +AttrArgs = h:AttrArg t:(COMMA @AttrArg)* { return [h].concat(t) } + / '' { return [] } +AttrArg = Ident / Number + +// Terminals +Ident = h:[a-zA-Z_] t:[0-9a-zA-Z_]* WS { return h + t.join('') } +Number = digits:[0-9]+ WS { return parseInt(digits.join(''), 10) } + +// Terminal symbols +LPAREN = '(' WS +RPAREN = ')' WS +COMMA = ',' WS +LATTR = '[[' WS +RATTR = ']]' WS + +// Misc +WS = [ \t\r\n]*