1
0
mirror of https://github.com/emkay/parser-6502.git synced 2024-12-22 06:29:27 +00:00
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,18 +1,13 @@
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.and(
mona.string('""'),
mona.value('"'))) mona.value('"')))
} }
function alphanum () {
return mona.join(
mona.value('alphanum'),
mona.string('background3')
)
}
function bit () { function bit () {
return mona.join( return mona.join(
mona.value('digit'), mona.value('digit'),
@ -68,9 +63,23 @@ function address () {
) )
} }
// this is the fallthrough parser
function alphanum () {
return mona.join(
mona.value('alphanum'),
mona.and(
mona.not(bit()),
mona.not(address()),
mona.not(binary()),
mona.not(bit()),
mona.not(string()),
mona.text(mona.alphanum())
)
)
}
function parameter () { function parameter () {
return mona.collect( return mona.or(
mona.or(
address(), address(),
binary(), binary(),
hex(), hex(),
@ -78,12 +87,10 @@ function parameter () {
bit(), bit(),
alphanum() 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()),
@ -91,7 +98,6 @@ function parameters () {
mona.spaces() mona.spaces()
) )
) )
)
} }
module.exports = { module.exports = {

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'
]
]) ])
}) })