This commit is contained in:
Michael Matuzak 2016-10-12 16:41:28 -07:00
parent e86204609b
commit dfe822b086
6 changed files with 30 additions and 13 deletions

5
example/index.js Normal file
View File

@ -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)

View File

@ -22,9 +22,17 @@ function directiveName () {
function directive () { function directive () {
return mona.sequence((s) => { return mona.sequence((s) => {
const d = s(directiveName()) const d = s(directiveName())
const space = s(mona.spaces()) const args = s(
const args = s(parameters()) mona.maybe(
const nl = s(mona.eol()) mona.and(
mona.spaces(),
mona.followedBy(
parameters(),
mona.eol()
)
)
)
)
return mona.value({ return mona.value({
directive: d, directive: d,

View File

@ -8,8 +8,8 @@ function quotedChar () {
mona.value('"'))) mona.value('"')))
} }
function bit () { function digit () {
return mona.digit(2) return mona.text(mona.digit(), {min: 1})
} }
function string () { function string () {
@ -45,7 +45,7 @@ function address () {
// this is the fallthrough parser // this is the fallthrough parser
function alphanum () { function alphanum () {
return mona.and( return mona.and(
mona.not(bit()), mona.not(digit()),
mona.not(address()), mona.not(address()),
mona.not(binary()), mona.not(binary()),
mona.not(hex()), mona.not(hex()),
@ -63,7 +63,7 @@ function parameter () {
mona.tag(binary(), 'binary'), mona.tag(binary(), 'binary'),
mona.tag(hex(), 'hex'), mona.tag(hex(), 'hex'),
mona.tag(string(), 'string'), mona.tag(string(), 'string'),
mona.tag(bit(), 'bit'), mona.tag(digit(), 'digit'),
mona.tag(alphanum(), 'alphanum') mona.tag(alphanum(), 'alphanum')
) )
} }
@ -71,11 +71,7 @@ function parameter () {
function parameters () { function parameters () {
return mona.split( return mona.split(
parameter(), parameter(),
mona.or( mona.trim(mona.string(','))
mona.and(mona.string(','), mona.spaces()),
mona.string(','),
mona.spaces()
)
) )
} }

View File

@ -7,7 +7,7 @@ tap.test('will parse a directive', (t) => {
t.deepEqual(mona.parse(directiveParser(), '.inesprg 1\n'), { t.deepEqual(mona.parse(directiveParser(), '.inesprg 1\n'), {
args: [ args: [
{ {
'bit': '1' 'digit': '1'
} }
], ],
directive: '.inesprg' directive: '.inesprg'

View File

@ -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) => { tap.test('should parse alphanum param', (t) => {
t.plan(1) t.plan(1)
const input = 'background3' const input = 'background3'