tagging types in the ast

This commit is contained in:
Michael Matuzak 2016-10-10 21:08:37 -07:00
parent 5e098fe69d
commit 353bab4dfd
3 changed files with 93 additions and 148 deletions

View File

@ -9,83 +9,61 @@ function quotedChar () {
} }
function bit () { function bit () {
return mona.join( return mona.digit(2)
mona.value('digit'),
mona.digit(2)
)
} }
function string () { function string () {
return mona.join( return mona.between(
mona.value('string'), mona.string('"'),
mona.between( mona.string('"'),
mona.string('"'), mona.text(quotedChar())
mona.string('"'),
mona.text(quotedChar())
)
) )
} }
function hex () { function hex () {
return mona.join( return mona.and(
mona.and( mona.string('#$'),
mona.string('#$'), mona.text(mona.digit(16))
mona.value('hex')
),
mona.text(
mona.digit(16)
)
) )
} }
function binary () { function binary () {
return mona.join( return mona.and(
mona.and( mona.maybe(mona.string('#')),
mona.maybe(mona.string('#')), mona.string('%'),
mona.string('%'), mona.text(mona.digit(2))
mona.value('binary')
),
mona.text(
mona.digit(2)
)
) )
} }
function address () { function address () {
return mona.join( return mona.and(
mona.and( mona.string('$'),
mona.string('$'), mona.text(mona.alphanum())
mona.value('address') )
), }
// this is the fallthrough parser
function alphanum () {
return mona.and(
mona.not(bit()),
mona.not(address()),
mona.not(binary()),
mona.not(hex()),
mona.not(string()),
mona.text( mona.text(
mona.alphanum() mona.alphanum()
) )
) )
} }
// 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.or( return mona.or(
address(), mona.tag(address(), 'address'),
binary(), mona.tag(binary(), 'binary'),
hex(), mona.tag(hex(), 'hex'),
string(), mona.tag(string(), 'string'),
bit(), mona.tag(bit(), 'bit'),
alphanum() mona.tag(alphanum(), 'alphanum')
) )
} }
@ -102,11 +80,5 @@ function parameters () {
module.exports = { module.exports = {
parameter, parameter,
parameters, parameters
address,
binary,
hex,
string,
bit,
alphanum
} }

View File

@ -6,10 +6,9 @@ tap.test('will parse a directive', (t) => {
t.plan(1) t.plan(1)
t.deepEqual(mona.parse(directiveParser(), '.inesprg 1'), { t.deepEqual(mona.parse(directiveParser(), '.inesprg 1'), {
args: [ args: [
[ {
'digit', 'bit': '1'
'1' }
]
], ],
directive: '.inesprg' directive: '.inesprg'
}) })
@ -19,10 +18,9 @@ tap.test('will parse a directive with direct address', (t) => {
t.plan(1) t.plan(1)
t.deepEqual(mona.parse(directiveParser(), '.inesprg $0000'), { t.deepEqual(mona.parse(directiveParser(), '.inesprg $0000'), {
args: [ args: [
[ {
'address', 'address': '0000'
'0000' }
]
], ],
directive: '.inesprg' directive: '.inesprg'
}) })
@ -32,10 +30,9 @@ tap.test('will parse a directive with hex arg', (t) => {
t.plan(1) t.plan(1)
t.deepEqual(mona.parse(directiveParser(), '.inesprg #$FE'), { t.deepEqual(mona.parse(directiveParser(), '.inesprg #$FE'), {
args: [ args: [
[ {
'hex', 'hex': 'FE'
'FE' }
]
], ],
directive: '.inesprg' directive: '.inesprg'
}) })
@ -45,20 +42,18 @@ tap.test('will parse a directive with binary arg', (t) => {
t.plan(2) t.plan(2)
t.deepEqual(mona.parse(directiveParser(), '.db %00010001'), { t.deepEqual(mona.parse(directiveParser(), '.db %00010001'), {
args: [ args: [
[ {
'binary', 'binary': '00010001'
'00010001' }
]
], ],
directive: '.db' directive: '.db'
}) })
t.deepEqual(mona.parse(directiveParser(), '.db #%00010001'), { t.deepEqual(mona.parse(directiveParser(), '.db #%00010001'), {
args: [ args: [
[ {
'binary', 'binary': '00010001'
'00010001' }
]
], ],
directive: '.db' directive: '.db'
}) })
@ -68,18 +63,15 @@ tap.test('will parse a directive with multiple args', (t) => {
t.plan(1) t.plan(1)
t.deepEqual(mona.parse(directiveParser(), '.db %00010001,%00010001,%00010001'), { t.deepEqual(mona.parse(directiveParser(), '.db %00010001,%00010001,%00010001'), {
args: [ args: [
[ {
'binary', 'binary': '00010001'
'00010001' },
], {
[ 'binary': '00010001'
'binary', },
'00010001' {
], 'binary': '00010001'
[ }
'binary',
'00010001'
]
], ],
directive: '.db' directive: '.db'
}) })
@ -89,19 +81,15 @@ tap.test('will parse a directive with multiple args with spaces between them', (
t.plan(1) t.plan(1)
t.deepEqual(mona.parse(directiveParser(), '.db %00010001, %00010001, %00010001'), { t.deepEqual(mona.parse(directiveParser(), '.db %00010001, %00010001, %00010001'), {
args: [ args: [
[ {
'binary', 'binary': '00010001'
'00010001' },
], {
[ 'binary': '00010001'
'binary', },
'00010001' {
], 'binary': '00010001'
[ }
'binary',
'00010001'
]
], ],
directive: '.db' directive: '.db'
}) })
@ -111,10 +99,9 @@ tap.test('will parse a directive with string arg', (t) => {
t.plan(1) t.plan(1)
t.deepEqual(mona.parse(directiveParser(), '.incbin "mario.chr"'), { t.deepEqual(mona.parse(directiveParser(), '.incbin "mario.chr"'), {
args: [ args: [
[ {
'string', 'string': 'mario.chr'
'mario.chr' }
]
], ],
directive: '.incbin' directive: '.incbin'
}) })

View File

@ -5,52 +5,41 @@ const parsers = require('../parsers/parameters')
tap.test('should parse direct memory address param', (t) => { tap.test('should parse direct memory address param', (t) => {
t.plan(1) t.plan(1)
const input = '$C000' const input = '$C000'
t.deepEqual(mona.parse(parsers.address(), input), [ t.deepEqual(mona.parse(parsers.parameter(), input), {
'address', 'address': 'C000'
'C000' })
])
}) })
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'
t.deepEqual(mona.parse(parsers.alphanum(), input), [ t.deepEqual(mona.parse(parsers.parameter(), input), {
'alphanum', 'alphanum': 'background3'
'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'
t.deepEqual(mona.parse(parsers.hex(), input), [ t.deepEqual(mona.parse(parsers.parameter(), input), {
'hex', 'hex': 'FF'
'FF' })
])
}) })
tap.test('should parse binary param', (t) => { tap.test('should parse binary param', (t) => {
t.plan(1) t.plan(1)
const input = '#%00000001' const input = '#%00000001'
t.deepEqual(mona.parse(parsers.binary(), input), [ t.deepEqual(mona.parse(parsers.parameter(), input), {
'binary', 'binary': '00000001'
'00000001' })
])
}) })
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' })
])
}) })
tap.test('should parse multiple params', (t) => { tap.test('should parse multiple params', (t) => {
@ -58,17 +47,14 @@ tap.test('should parse multiple params', (t) => {
const input = '$24,$24,$24' const input = '$24,$24,$24'
t.deepEqual(mona.parse(parsers.parameters(), input), [ t.deepEqual(mona.parse(parsers.parameters(), input), [
[ {
'address', 'address': '24'
'24' },
], {
[ 'address': '24'
'address', },
'24' {
], 'address': '24'
[ }
'address',
'24'
]
]) ])
}) })