STUDIO HAZE · topaz
Topaz source
Open by house policy. Topaz is our own language.
Topaz 5.20 · 56 files
compiler/crates/topaz_self_frontend/topaz/parser.tpz
Topaz · 2712 B · Original file
1
// Parse facade over the self compiler's token and grammar leaves.2
// Flattening stays here because it shapes the public preview tree, while parser3
// state, recovery, and grammar families remain below parser/.5
import types { ParseResult, PreviewAstNode, PreviewToken, SyntaxNode }6
import parser.core { Parser, ParserRequest, ParserService }7
import parser.declarations_modules { parseBlock, parseProgram }8
import parser.expressions { parseExpressionBp }10
function dispatchParserService(11
parser: Parser,12
request: ParserRequest,13
) -> Result<SyntaxNode, string> {14
if request.kind == "parse-block" {15
return parseBlock(16
dispatchParserService,17
parser,18
request.field,19
request.index,20
)21
}22
parseExpressionBp(23
dispatchParserService,24
parser,25
request.precedence,26
request.field,27
request.index,28
)29
}31
function parserService() -> ParserService {32
dispatchParserService33
}35
function flatten(36
value: SyntaxNode,37
parent: int,38
depth: int,39
maxNodes: int,40
maxDepth: int,41
output: Array<PreviewAstNode>,42
) -> Result<int, string> {43
if depth > maxDepth {44
return Err("front-end preview AST depth exceeds {maxDepth}")45
}46
if output.length >= maxNodes {47
return Err("front-end preview AST node count exceeds {maxNodes}")48
}49
let mut rows = output50
let ordinal = output.length51
let mut attributeNames: Array<string> = []52
let mut attributeKinds: Array<string> = []53
let mut attributeValues: Array<string> = []54
for attribute in value.attributes {55
attributeNames.push(attribute.name)56
attributeKinds.push(attribute.kind)57
attributeValues.push(attribute.value)58
}59
rows.push(PreviewAstNode {60
kind: value.kind,61
lo: value.lo,62
hi: value.hi,63
parent: parent,64
field: value.field,65
index: value.index,66
attributeNames: attributeNames,67
attributeKinds: attributeKinds,68
attributeValues: attributeValues,69
})70
for child in value.children {71
flatten(child, ordinal, depth + 1, maxNodes, maxDepth, output)?72
}73
Ok(ordinal)74
}76
// Parses a normalized token stream into the bounded preview AST.77
export function parse(78
tokens: Array<PreviewToken>,79
source: string,80
maxNodes: int,81
maxDepth: int,82
) -> Result<ParseResult, string> {83
let bytes = Bytes.encodeUtf8(source)84
let parser = Parser {85
source: bytes,86
tokens: tokens,87
position: [0],88
lastHi: [0],89
pendingGt: [],90
nakedLambda: [true],91
recordUpdate: [true],92
diagnostics: [],93
}94
let root = parseProgram(parserService(), parser, bytes.length())95
let mut nodes: Array<PreviewAstNode> = []96
flatten(root, -1, 0, maxNodes, maxDepth, nodes)?97
Ok(ParseResult { root: root, nodes: nodes, diagnostics: parser.diagnostics })98
}SHA256 16c9f8df6dfab15d7bf0697e57a45487ebece8ce936a3c4c00df19108a984517