1
0
mirror of https://github.com/emkay/parser-6502.git synced 2024-10-15 02:24:12 +00:00

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

View File

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

View File

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