Parser 2.0 (Major Delezer) #52

Merged
defunkt merged 35 commits from parser2 into main 2025-12-08 16:35:34 +00:00
2 changed files with 18 additions and 15 deletions
Showing only changes of commit d003d65a15 - Show all commits

View File

@ -1,9 +1,10 @@
import { CompilerError } from '#compiler/compilerError.ts'
import { parse } from '#parser/parser2'
import { SyntaxNode, Tree } from '#parser/node'
import { parser } from '#parser/shrimp.ts'
import * as terms from '#parser/shrimp.terms'
import { setGlobals } from '#parser/tokenizer'
import { tokenizeCurlyString } from '#parser/curlyTokenizer'
import type { SyntaxNode, Tree } from '@lezer/common'
import { assert, errorMessage } from '#utils/utils'
import { toBytecode, type Bytecode, type ProgramItem, bytecodeToString } from 'reefvm'
import {
@ -63,13 +64,14 @@ export class Compiler {
constructor(public input: string, globals?: string[] | Record<string, any>) {
try {
if (globals) setGlobals(Array.isArray(globals) ? globals : Object.keys(globals))
const cst = parser.parse(input)
const errors = checkTreeForErrors(cst)
const ast = parse(input)
const cst = new Tree(ast)
// const errors = checkTreeForErrors(cst)
const firstError = errors[0]
if (firstError) {
throw firstError
}
// const firstError = errors[0]
// if (firstError) {
// throw firstError
// }
this.#compileCst(cst, input)
this.bytecode = toBytecode(this.instructions)

View File

@ -1,16 +1,17 @@
import { CompilerError } from '#compiler/compilerError.ts'
import * as terms from '#parser/shrimp.terms'
import type { SyntaxNode, Tree } from '@lezer/common'
import type { SyntaxNode, Tree } from '#parser/node'
export const checkTreeForErrors = (tree: Tree): CompilerError[] => {
const errors: CompilerError[] = []
tree.iterate({
enter: (node) => {
if (node.type.isError) {
errors.push(new CompilerError(`Unexpected syntax.`, node.from, node.to))
}
},
})
// tree.iterate({
// enter: (node) => {
// if (node.type.isError) {
// errors.push(new CompilerError(`Unexpected syntax.`, node.from, node.to))
// }
// },
// })
return errors
}