This commit is contained in:
Michael Matuzak 2016-10-07 15:54:31 -07:00
parent cb0e34d1e3
commit 5e098fe69d
5 changed files with 58 additions and 37 deletions

View File

@ -25,7 +25,7 @@ function instructionName () {
function instruction () { function instruction () {
return mona.sequence((s) => { return mona.sequence((s) => {
const i = s(instructionName()) const i = s(instructionName())
const space = s(mona.spaces()) const space = s(mona.maybe(mona.spaces()))
const args = s(mona.maybe(parameters())) const args = s(mona.maybe(parameters()))
// const nl = s(mona.eol()) // const nl = s(mona.eol())
return mona.value({ return mona.value({

View File

@ -1,16 +1,11 @@
const mona = require('mona') const mona = require('mona')
function quotedChar () { function quotedChar () {
return mona.or(mona.noneOf('"'), return mona.or(
mona.and(mona.string('""'), mona.noneOf('"'),
mona.value('"'))) mona.and(
} mona.string('""'),
mona.value('"')))
function alphanum () {
return mona.join(
mona.value('alphanum'),
mona.string('background3')
)
} }
function bit () { function bit () {
@ -68,28 +63,39 @@ function address () {
) )
} }
function parameter () { // this is the fallthrough parser
return mona.collect( function alphanum () {
mona.or( return mona.join(
address(), mona.value('alphanum'),
binary(), mona.and(
hex(), mona.not(bit()),
string(), mona.not(address()),
bit(), mona.not(binary()),
alphanum() mona.not(bit()),
mona.not(string()),
mona.text(mona.alphanum())
) )
) )
} }
function parameter () {
return mona.or(
address(),
binary(),
hex(),
string(),
bit(),
alphanum()
)
}
function parameters () { function parameters () {
return mona.map(a => a.map(b => b[0]), return mona.split(
mona.split( parameter(),
parameter(), mona.or(
mona.or( mona.and(mona.string(','), mona.spaces()),
mona.and(mona.string(','), mona.spaces()), mona.string(','),
mona.string(','), mona.spaces()
mona.spaces()
)
) )
) )
} }

View File

@ -8,7 +8,10 @@ tap.test('should parse the basics', (t) => {
{ {
directive: '.org', directive: '.org',
args: [ args: [
'$,C000' [
'address',
'C000'
]
] ]
}, },
{ {

View File

@ -4,7 +4,7 @@ const instructionParser = require('../parsers/instruction')
tap.test('will parse an instruction', (t) => { tap.test('will parse an instruction', (t) => {
t.plan(1) t.plan(1)
t.deepEqual(mona.parse(instructionParser(), 'sei'), { t.deepEqual(mona.parse(instructionParser(), 'sei\n'), {
args: null, args: null,
instruction: 'sei' instruction: 'sei'
}) })
@ -12,7 +12,7 @@ tap.test('will parse an instruction', (t) => {
tap.test('will parse an instruction with args', (t) => { tap.test('will parse an instruction with args', (t) => {
t.plan(1) t.plan(1)
t.deepEqual(mona.parse(instructionParser(), 'stx $2000'), { t.deepEqual(mona.parse(instructionParser(), 'stx $2000\n'), {
args: [ args: [
[ [
'address', 'address',
@ -25,7 +25,7 @@ tap.test('will parse an instruction with args', (t) => {
tap.test('will parse an instruction with multiple args', (t) => { tap.test('will parse an instruction with multiple args', (t) => {
t.plan(1) t.plan(1)
t.deepEqual(mona.parse(instructionParser(), 'lda background3, x'), { t.deepEqual(mona.parse(instructionParser(), 'lda background3, x\n'), {
args: ['background3', 'x'], args: ['background3', 'x'],
instruction: 'lda' instruction: 'lda'
}) })

View File

@ -11,6 +11,21 @@ tap.test('should parse direct memory address param', (t) => {
]) ])
}) })
tap.test('should parse alphanum param', (t) => {
t.plan(1)
const input = 'background3'
t.deepEqual(mona.parse(parsers.alphanum(), input), [
'alphanum',
'background3'
])
})
tap.test('should not parse alphanum param if empty', (t) => {
t.plan(1)
const input = ''
t.notOk(mona.parse(parsers.alphanum(), input))
})
tap.test('should parse hex param', (t) => { tap.test('should parse hex param', (t) => {
t.plan(1) t.plan(1)
const input = '#$FF' const input = '#$FF'
@ -31,13 +46,10 @@ tap.test('should parse binary param', (t) => {
tap.test('should parse param', (t) => { tap.test('should parse param', (t) => {
t.plan(1) t.plan(1)
const input = '#%00000001' const input = '#%00000001'
t.deepEqual(mona.parse(parsers.parameter(), input), [ t.deepEqual(mona.parse(parsers.parameter(), input), [
[ 'binary',
'binary', '00000001'
'00000001'
]
]) ])
}) })