Skip to content

Commit 87d2f38

Browse files
committed
[syntax] test parser by parse command
1 parent d2011bf commit 87d2f38

File tree

9 files changed

+107
-28
lines changed

9 files changed

+107
-28
lines changed

TODO.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# syntax
2-
3-
[syntax] test parser by parse command
4-
51
# builtins
62

73
`@send`

src/command-line/commands/Parse.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { ParsingError } from "@cicada-lang/partech/lib/errors"
2+
import { Command, CommandRunner } from "@xieyuheng/command-line"
3+
import { ty } from "@xieyuheng/ty"
4+
import fs from "node:fs"
5+
import { relative } from "node:path"
6+
import process from "node:process"
7+
import { Fetcher } from "../../fetcher"
8+
import { Report } from "../../lang/errors/Report"
9+
import { parseStmts } from "../../lang/syntax"
10+
import { createURL } from "../../utils/createURL"
11+
12+
type Args = { path: string }
13+
type Opts = {}
14+
15+
export class Parse extends Command<Args, Opts> {
16+
name = "parse"
17+
18+
description = "Parse an inet program"
19+
20+
args = { path: ty.string() }
21+
22+
// prettier-ignore
23+
help(runner: CommandRunner): string {
24+
const { blue } = this.colors
25+
26+
return [
27+
`Parse a file:`,
28+
``,
29+
blue(` ${runner.name} ${this.name} std/datatype/Nat.test.i`),
30+
``,
31+
].join("\n")
32+
}
33+
34+
async execute(argv: Args & Opts): Promise<void> {
35+
const fetcher = new Fetcher()
36+
37+
fetcher.register("file", {
38+
async fetchText(url) {
39+
if (process.platform === "win32") {
40+
return await fs.promises.readFile(url.pathname.slice(1), "utf8")
41+
} else {
42+
return await fs.promises.readFile(url.pathname, "utf8")
43+
}
44+
},
45+
46+
formatURL(url) {
47+
if (process.platform === "win32") {
48+
return relative(process.cwd(), url.pathname.slice(1)).replaceAll(
49+
"\\",
50+
"/",
51+
)
52+
} else {
53+
return relative(process.cwd(), url.pathname)
54+
}
55+
},
56+
})
57+
58+
const url = createURL(argv.path)
59+
const text = await fetcher.fetchText(url)
60+
61+
try {
62+
const stmts = parseStmts(text)
63+
for (const stmt of stmts) {
64+
console.dir(stmt, { depth: null })
65+
}
66+
} catch (error) {
67+
if (error instanceof ParsingError) {
68+
console.error(error.report(text))
69+
process.exit(1)
70+
} else if (error instanceof Report) {
71+
console.error(error.format())
72+
process.exit(1)
73+
} else {
74+
throw error
75+
}
76+
}
77+
}
78+
}

src/command-line/commands/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export * from "@xieyuheng/command-line/lib/commands"
22
export * from "./Default"
33
// export * from "./Format"
4-
// export * from "./Parse"
4+
export * from "./Parse"
55
// export * from "./Repl"
66
export * from "./Run"

src/command-line/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function createCommandRunner(): CommandRunner {
77
commands: [
88
// new Commands.Repl(),
99
new Commands.Run(),
10-
// new Commands.Parse(),
10+
new Commands.Parse(),
1111
// new Commands.Format(),
1212
new Commands.CommonHelp(),
1313
],

src/lang/syntax/grammars/block_stmt.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
export const block_stmt = {
22
$grammar: {
33
"block_stmt:connect": [
4-
"'('",
4+
'"("',
55
{ inputArgs: "args" },
6-
"')'",
7-
"'-'",
8-
"'>'",
9-
"'['",
6+
'")"',
7+
'"-"',
8+
'">"',
9+
'"["',
1010
{ transition: "exp" },
11-
"']'",
12-
"'-'",
13-
"'>'",
14-
"'('",
11+
'"]"',
12+
'"-"',
13+
'">"',
14+
'"("',
1515
{ outputArgs: "args" },
16-
"')'",
16+
'")"',
1717
],
1818
"block_stmt:let_place": [
1919
'"place"',

src/lang/syntax/grammars/parameter.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
export const parameter = {
22
$grammar: {
3-
"parameter:normal": [{ name: "variable_name" }, '":"', { t: "exp" }],
3+
"parameter:place": [
4+
'"place"',
5+
{ name: "variable_name" },
6+
'":"',
7+
{ t: "exp" },
8+
],
49
},
510
}
611

src/lang/syntax/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// import * as pt from "@cicada-lang/partech"
2-
// import * as grammars from "./grammars"
3-
// import * as matchers from "./matchers"
1+
import * as pt from "@cicada-lang/partech"
2+
import * as grammars from "./grammars"
3+
import * as matchers from "./matchers"
44

55
/**
66
@@ -10,9 +10,9 @@
1010
1111
**/
1212

13-
// export const parseStmts = pt.gen_parse({
14-
// preprocess: pt.preprocess.erase_comment,
15-
// lexer: pt.lexers.common,
16-
// grammar: pt.grammar_start(grammars, "stmts"),
17-
// matcher: matchers.stmts_matcher,
18-
// })
13+
export const parseStmts = pt.gen_parse({
14+
preprocess: pt.preprocess.erase_comment,
15+
lexer: pt.lexers.common,
16+
grammar: pt.grammar_start(grammars, "stmts"),
17+
matcher: matchers.stmts_matcher,
18+
})

src/lang/syntax/matchers/parameter_matcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as matchers from "../matchers"
44

55
export function parameter_matcher(tree: pt.Tree): ParameterExp {
66
return pt.matcher<ParameterExp>({
7-
"parameter:normal": ({ name, t }) => ({
7+
"parameter:place": ({ name, t }) => ({
88
name: pt.str(name),
99
t: matchers.exp_matcher(t),
1010
isPrincipal: false,

src/lang/syntax/matchers/stmt_matcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as matchers from "../matchers"
44

55
export function stmt_matcher(tree: pt.Tree): Stmt {
66
return pt.matcher<Stmt>({
7-
"stmt:function": (
7+
"stmt:transition": (
88
{ name, inputParameters, outputParameters, body },
99
{ span },
1010
) => ({

0 commit comments

Comments
 (0)