From dfe822b086e724b4b87ac177c0af6d50727ed44a Mon Sep 17 00:00:00 2001 From: Michael Matuzak Date: Wed, 12 Oct 2016 16:41:28 -0700 Subject: [PATCH] working --- example.s => example/example.s | 0 example/index.js | 5 +++++ parsers/directive.js | 14 +++++++++++--- parsers/parameters.js | 14 +++++--------- test/directive.js | 2 +- test/parameters.js | 8 ++++++++ 6 files changed, 30 insertions(+), 13 deletions(-) rename example.s => example/example.s (100%) create mode 100644 example/index.js diff --git a/example.s b/example/example.s similarity index 100% rename from example.s rename to example/example.s diff --git a/example/index.js b/example/index.js new file mode 100644 index 0000000..7c3b055 --- /dev/null +++ b/example/index.js @@ -0,0 +1,5 @@ +const fs = require('fs') +const parser = require('.') +const input = fs.readFileSync('./example.s', 'utf-8') +const r = parser(input) +console.log(r) diff --git a/parsers/directive.js b/parsers/directive.js index 775dce8..80c1f48 100644 --- a/parsers/directive.js +++ b/parsers/directive.js @@ -22,9 +22,17 @@ function directiveName () { function directive () { return mona.sequence((s) => { const d = s(directiveName()) - const space = s(mona.spaces()) - const args = s(parameters()) - const nl = s(mona.eol()) + const args = s( + mona.maybe( + mona.and( + mona.spaces(), + mona.followedBy( + parameters(), + mona.eol() + ) + ) + ) + ) return mona.value({ directive: d, diff --git a/parsers/parameters.js b/parsers/parameters.js index 0d12d73..aa4a4ae 100644 --- a/parsers/parameters.js +++ b/parsers/parameters.js @@ -8,8 +8,8 @@ function quotedChar () { mona.value('"'))) } -function bit () { - return mona.digit(2) +function digit () { + return mona.text(mona.digit(), {min: 1}) } function string () { @@ -45,7 +45,7 @@ function address () { // this is the fallthrough parser function alphanum () { return mona.and( - mona.not(bit()), + mona.not(digit()), mona.not(address()), mona.not(binary()), mona.not(hex()), @@ -63,7 +63,7 @@ function parameter () { mona.tag(binary(), 'binary'), mona.tag(hex(), 'hex'), mona.tag(string(), 'string'), - mona.tag(bit(), 'bit'), + mona.tag(digit(), 'digit'), mona.tag(alphanum(), 'alphanum') ) } @@ -71,11 +71,7 @@ function parameter () { function parameters () { return mona.split( parameter(), - mona.or( - mona.and(mona.string(','), mona.spaces()), - mona.string(','), - mona.spaces() - ) + mona.trim(mona.string(',')) ) } diff --git a/test/directive.js b/test/directive.js index dbbb371..1ffb121 100644 --- a/test/directive.js +++ b/test/directive.js @@ -7,7 +7,7 @@ tap.test('will parse a directive', (t) => { t.deepEqual(mona.parse(directiveParser(), '.inesprg 1\n'), { args: [ { - 'bit': '1' + 'digit': '1' } ], directive: '.inesprg' diff --git a/test/parameters.js b/test/parameters.js index 0784f31..672d4a5 100644 --- a/test/parameters.js +++ b/test/parameters.js @@ -10,6 +10,14 @@ tap.test('should parse direct memory address param', (t) => { }) }) +tap.test('should parse decimal param', (t) => { + t.plan(1) + const input = '17' + t.deepEqual(mona.parse(parsers.parameter(), input), { + 'digit': '17' + }) +}) + tap.test('should parse alphanum param', (t) => { t.plan(1) const input = 'background3'