Skip to content

Commit

Permalink
Successfully parsed ometa1 string grammar by ometa parser itself.
Browse files Browse the repository at this point in the history
  • Loading branch information
maestrow committed May 19, 2021
1 parent 1545026 commit 0c76a47
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
13 changes: 13 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { math1 } from './grammars/math1'
import { ometa1, proj } from './grammars/ometa1'
import { TraceDiscovery } from "./utils/trace-discover";
import { IParserFn } from "types";
import * as equal from "fast-deep-equal/es6";

const printTrace = (p: Parser, input) => {
const disco = new TraceDiscovery(p.grammar, input, p.trace.data)
Expand All @@ -28,5 +29,17 @@ const printRes = (p: Parser, res: IMatchResult) => {
const input = fs.readFileSync('src/grammars/ometa1.ometa', 'utf-8')
const p = new Parser(ometa1, proj)
const r = p.match(input as unknown as any[], 'ometa')

saveTrace(p, input)
printRes(p, r)
const isEqual = equal(ometa1, r.result)

console.log('\nDiff:\n')
console.log('isEqual: ' + isEqual)

fs.writeFileSync("./dist/diff_expected.json", JSON.stringify(ometa1, null, 2))
fs.writeFileSync("./dist/diff_actual.json", JSON.stringify(r.result, null, 2))




2 changes: 1 addition & 1 deletion src/grammars/ometa1.ometa
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ometa Ometa1 {
eTop = eAlt,
eAlt = eProj ("|" eProj)*,
eProj = eSeq ("->" ident)?,
eSeq = eQuant (spaces1 quant)*,
eSeq = eQuant (spaces1 eQuant)*,
eQuant = eNot ('?' | '*' | '+')?,
eNot = '~'? operand,

Expand Down
10 changes: 10 additions & 0 deletions src/grammars/ometa1.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as fs from "fs";
import { Parser } from "../parser";
import { ometa1, proj } from './ometa1'

test('ometa1', () => {
const input = fs.readFileSync('src/grammars/ometa1.ometa', 'utf-8')
const p = new Parser(ometa1, proj)
const r = p.match(input as unknown as any[], 'ometa')
expect(r.result).toEqual(ometa1)
})
20 changes: 12 additions & 8 deletions src/grammars/ometa1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const ometa1: AST.Grammar = [
]]],

['operand', ['alt', [
['project', 'op_group', ['seq', [
['project', 'opGroup', ['seq', [
['token', '('],
['rule', 'eTop'],
['token', ')'],
Expand Down Expand Up @@ -135,14 +135,16 @@ export const ometa1: AST.Grammar = [
]


const x = (value) => value.map(i => i[2]).join('')
const strMap = (value) => value.map(i => i[2]).join('')
const rxMap = (value) => value.map(i => (i[1].length ? '\\' : '') + i[2]).join('')
const internalRules = ['anything']

export const proj: IProjectors = {
ometa: ([_1, ident, _2, rules]) => rules,

eRule: ([ident, _2, _3, _4, expr]) => [ident, expr],
eAlt: ([first, rest]) => rest.length ? ['alt', [first, ...(rest.map(i => i[1]))]] : first,
eProj: ([value, proj]) => proj.length ? ['project', proj[1], value] : value,
eProj: ([expr, proj]) => proj.length ? ['project', proj[0][1], expr] : expr,
eSeq: ([first, rest]) => rest.length ? ['seq', [first, ...(rest.map(i => i[1]))]] : first,
eQuant: ([value, op]) => {
if (op.length) {
Expand All @@ -156,12 +158,14 @@ export const proj: IProjectors = {
},
eNot: ([op, value]) => op.length ? ['not', value] : value,

opGroup: ([_1, value]) => ['seq', value],
opRule: (ident) => ['rule', ident],
opGroup: ([_1, value]) => value, // What abount grouping as ['seq', value] ?
opRule: (ident) => internalRules.includes(ident as unknown as string)
? [ ident ]
: ['rule', ident],

eRange: ([_1, from, _2, to]) => ['range', from, to],
eStr: ([_1, value]) => ['equal', x(value)],
eToken: ([_1, value]) => ['token', x(value)],
eRegex: ([_1, value, _2, modif]) => ['regex', x(value)],
eStr: ([_1, value]) => ['equal', strMap(value)],
eToken: ([_1, value]) => ['token', strMap(value)],
eRegex: ([_1, value, _2, modif]) => ['regex', rxMap(value)],
ident: ([_, first, rest]) => first + rest.join(''),
}

0 comments on commit 0c76a47

Please sign in to comment.