Skip to content

Commit

Permalink
step2 debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
maestrow committed May 13, 2021
1 parent 4438c77 commit 1296d6f
Show file tree
Hide file tree
Showing 12 changed files with 323 additions and 120 deletions.
21 changes: 0 additions & 21 deletions src/grammars/expr.ometa

This file was deleted.

55 changes: 0 additions & 55 deletions src/grammars/math.ometa

This file was deleted.

13 changes: 0 additions & 13 deletions src/grammars/math2.ometa

This file was deleted.

4 changes: 3 additions & 1 deletion src/grammars/ometa1.ometa
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,10 @@ ometa Ometa {
rule
equal

times
token
times
not
bind
project
range
}
2 changes: 1 addition & 1 deletion src/step1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ const grammar: AST.Grammar = [
]


const p = new Parser(grammar, [...'((1+2)-3*3)/4'])
const p = new Parser(grammar, '((1+2)-3*3)/4' as unknown as any[])

const r = p.match('expr')

Expand Down
2 changes: 1 addition & 1 deletion src/step2/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Parser } from './parser'
import { GrammarAst as AST } from './grammar-ast'
import { Ast as AST } from './grammar-ast'
import { math1 } from './grammars/math1'

const p = new Parser(math1, [...'((1+2)-3*3)/4'])
Expand Down
33 changes: 26 additions & 7 deletions src/step2/grammar-ast.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
export namespace GrammarAst {
export namespace Ast {
export type Grammar = Rule[]
export type Rule = [string, Expr]
export type Expr = ExSeq | ExAlt | ExAtom | ExRule | ExNotLess
export type ExSeq = ['seq', Expr[]]
export type ExAlt = ['alt', Expr[]]

export type Expr =
Ex.Seq
| Ex.Alt
| Ex.Atom
| Ex.Rule
| Ex.Times
| Ex.Token
| Ex.Not
| Ex.Project
| Ex.Regex
| Ex.Range

export type ExAtom = ['equal', string]
export type ExRule = ['rule', string]
export type ExNotLess = ['notLess', number, Expr]
export namespace Ex {
export type Seq = ['seq', Expr[]]
export type Alt = ['alt', Expr[]]
export type Atom = ['equal', string]
export type Rule = ['rule', string]
export type Times = ['times', number, number, Expr]

export type Token = ['token', string]
export type Not = ['not', Expr]
export type Project = ['project', string, Expr]
export type Regex = ['regex', string]
export type Range = ['range', string, string]
}
}
18 changes: 15 additions & 3 deletions src/step2/grammars/math1.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { GrammarAst as AST } from '../grammar-ast'
import { Ast as AST } from '../grammar-ast'

/*
ometa {
expr = group (op group)*,
group = '(' expr ')' | num,
op = '-'|'+'|'*'|'/',
num = digit+,
digit = '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'
}
*/

export const math1: AST.Grammar = [
['expr', ['seq', [
['rule', 'group'],
['notLess', 0, ['seq', [
['times', 0, null, ['seq', [
['rule', 'op'],
['rule', 'group']
]]]
Expand All @@ -22,7 +34,7 @@ export const math1: AST.Grammar = [
['equal', '*'],
['equal', '/'],
]]],
['num', ['notLess', 1, ['rule', 'digit']]],
['num', ['times', 1, null, ['rule', 'digit']]],
['digit', ['alt', [
['equal', '0'],
['equal', '1'],
Expand Down
164 changes: 164 additions & 0 deletions src/step2/grammars/ometa1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { IProjectors } from 'step2/types'
import { Ast as AST } from '../grammar-ast'

/*
Выражения располагаются в порядке приоритета (внизу - наибольший приоритет)
ometa Ometa1 {
ometa = "ometa" ident "{" eRule* "}",
eRule = ident "=" "|"? eTop inlSpaces* ',' newline,
eTop = eAlt,
eAlt = eProj ("|" eProj)*,
eProj = eSeq ("->" ident)?,
eSeq = eQuant (spaces quant)*,
eQuant = eNot ('?' | '*' | '+')?,
eNot = '~'? operand,
operand =
| '(' eTop ') -> op_group
| eRange -> op_range
| eStr -> op_str
| eToken -> op_token
| eRegex -> op_regex
| ident -> op_rule,
eRange = alphanum '-' alphanum,
eStr = "'" (~'\'' anything)* '\'',
eToken = "\"" (~'"' anything)* '"',
eRegex = "/" (~'/' anything)* '/' a-z*,
ident = letter alphanum*,
alphanum = letter | digit,
digit = 0-9,
letter = A-Z | a-z,
spaces = /\s+/,
space = /\s/,
inlSpaces = /[ \t]+/,
inlSpace = /[ \t]/,
newline = /(\r\n)|\n/,
}
*/

export const ometaExpr: AST.Grammar = [
['ometa', ['seq', [
['token', 'ometa'],
['rule', 'ident'],
['token', '{'],
['times', 0, null, ['rule', 'eRule']],
['token', '}'],
]]],

['rule', ['seq', [
['rule', 'ident'],
['token', '='],
['times', 0, 1, ['token', '|']],
['rule', 'eTop'],
['times', 0, null, ['rule', 'inlSpaces']],
['equal', ','],
['rule', 'newline'],
]]],

['eTop', ['rule', 'eAlt']],

['eAlt', ['seq', [
['rule', 'eProj'],
['times', 0, null, ['seq', [['equal', '|'], ['rule', 'eProj']]]]
]]],

['eProj', ['seq', [
['rule', 'eSeq'],
['times', 0, 1, ['seq', [['equal', '->'], ['rule', 'ident']]]],
]]],

['eSeq', ['seq', [
['rule', 'eQuant'],
['times', 0, null, ['seq', [['rule', 'spaces'], ['rule', 'eQuant']]]],
]]],

['eQuant', ['seq', [
['rule', 'not'],
['times', 0, 1, ['alt', [
['equal', '?'],
['equal', '*'],
['equal', '+'],
]]]
]]],

['eNot', ['seq', [
['times', 0, 1, ['equal', '~']],
['rule', 'operand']
]]],

['operand', ['alt', [
['project', 'op_group', ['seq', [
['equal', '('],
['rule', 'eTop'],
['equal', ')'],
]]],
['project', 'op_range', ['rule', 'eRange']],
['project', 'op_str', ['rule', 'eStr']],
['project', 'op_token', ['rule', 'eToken']],
['project', 'op_regex', ['rule', 'eRegex']],
['project', 'op_rule', ['rule', 'ident']],
]]],

['eRange', ['seq', [
['rule', 'alphanum'],
['equal', '-'],
['rule', 'alphanum'],
]]],

['eStr', ['seq', [
['token', '\''],
['times', 0, null, ['seq', [['not', ['equal', '\'']], ['rule', 'anything']]]],
['equal', '\''],
]]],

['eToken', ['seq', [
['token', '"'],
['times', 0, null, ['seq', [['not', ['equal', '"']], ['rule', 'anything']]]],
['equal', '"'],
]]],

['eRegex', ['seq', [
['token', '/'],
['times', 0, null, ['seq', [['not', ['equal', '/']], ['rule', 'anything']]]],
['equal', '/'],
['times', 0, null, ['range', 'a', 'z']],
]]],

['ident', ['seq', [
['rule', 'letter'],
['times', 0, null, ['rule', 'alphanum']]
]]],

['alphanum', ['alt', [['rule', 'letter'], ['rule', 'digit']]]],

['digit', ['range', '0', '9'],],

['letter', ['alt', [
['range', 'A', 'Z'],
['range', 'a', 'z'],
]]],

['spaces', ['regex', '\\s+']],

['space', ['regex', '\\s']],

['inlSpaces', ['regex', '[ \\t]+']],

['inlSpace', ['regex', '[ \\t]']],

['newline', ['regex', '(\\r\\n)|\\n']],
]


export const proj: IProjectors = {

}
Loading

0 comments on commit 1296d6f

Please sign in to comment.