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 () {
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,

View File

@ -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(','))
)
}

View File

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

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) => {
t.plan(1)
const input = 'background3'