Parser 2.0 (Major Delezer) #52
|
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/env bun
|
||||
|
||||
import { colors, globals as prelude } from '../src/prelude'
|
||||
import { treeToString } from '../src/utils/tree'
|
||||
import { treeToString2 } from '../src/utils/tree'
|
||||
import { runCode, runFile, compileFile, parseCode } from '../src'
|
||||
import { resolve } from 'path'
|
||||
import { bytecodeToString } from 'reefvm'
|
||||
|
|
@ -143,7 +143,7 @@ async function main() {
|
|||
process.exit(1)
|
||||
}
|
||||
const input = readFileSync(file, 'utf-8')
|
||||
console.log(treeToString(parseCode(input), input))
|
||||
console.log(treeToString2(parseCode(input), input))
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
16
src/index.ts
16
src/index.ts
|
|
@ -1,15 +1,15 @@
|
|||
import { readFileSync } from 'fs'
|
||||
import { VM, fromValue, toValue, isValue, type Bytecode } from 'reefvm'
|
||||
import { type Tree } from '@lezer/common'
|
||||
import { Compiler } from '#compiler/compiler'
|
||||
import { parser } from '#parser/shrimp'
|
||||
import { parse } from '#parser/parser2'
|
||||
import { type SyntaxNode, Tree } from '#parser/node'
|
||||
import { globals as parserGlobals, setGlobals as setParserGlobals } from '#parser/tokenizer'
|
||||
import { globals as prelude } from '#prelude'
|
||||
|
||||
export { Compiler } from '#compiler/compiler'
|
||||
export { parser } from '#parser/shrimp'
|
||||
export { parse } from '#parser/parser2'
|
||||
export { type SyntaxNode, Tree } from '#parser/node'
|
||||
export { globals as prelude } from '#prelude'
|
||||
export type { Tree } from '@lezer/common'
|
||||
export { type Value, type Bytecode } from 'reefvm'
|
||||
export { toValue, fromValue, isValue, Scope, VM, bytecodeToString } from 'reefvm'
|
||||
|
||||
|
|
@ -41,7 +41,7 @@ export class Shrimp {
|
|||
return isValue(result) ? fromValue(result, this.vm) : result
|
||||
}
|
||||
|
||||
parse(code: string): Tree {
|
||||
parse(code: string): SyntaxNode {
|
||||
return parseCode(code, this.globals)
|
||||
}
|
||||
|
||||
|
|
@ -95,17 +95,17 @@ export function compileCode(code: string, globals?: Record<string, any>): Byteco
|
|||
return compiler.bytecode
|
||||
}
|
||||
|
||||
export function parseFile(path: string, globals?: Record<string, any>): Tree {
|
||||
export function parseFile(path: string, globals?: Record<string, any>): SyntaxNode {
|
||||
const code = readFileSync(path, 'utf-8')
|
||||
return parseCode(code, globals)
|
||||
}
|
||||
|
||||
export function parseCode(code: string, globals?: Record<string, any>): Tree {
|
||||
export function parseCode(code: string, globals?: Record<string, any>): SyntaxNode {
|
||||
const oldGlobals = [...parserGlobals]
|
||||
const globalNames = [...Object.keys(prelude), ...(globals ? Object.keys(globals) : [])]
|
||||
|
||||
setParserGlobals(globalNames)
|
||||
const result = parser.parse(code)
|
||||
const result = parse(code)
|
||||
setParserGlobals(oldGlobals)
|
||||
|
||||
return result
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user