Add comments to the tree #24

Merged
probablycorey merged 5 commits from comments-in-tree into main 2025-11-06 00:19:35 +00:00
2 changed files with 11 additions and 6 deletions
Showing only changes of commit 146d2a22ee - Show all commits

View File

@ -657,7 +657,7 @@ export class Compiler {
}
case terms.Array: {
const children = getAllChildren(node).filter((n) => n.type.id !== terms.Comment)
const children = getAllChildren(node)

Maybe put the filter in getAllChildren() so we won't have to worry about it?

Maybe put the filter in getAllChildren() so we won't have to worry about it?

YES

YES
// We can easily parse [=] as an empty dict, but `[ = ]` is tougher.
// = can be a valid word, and is also valid inside words, so for now we cheat
@ -676,7 +676,7 @@ export class Compiler {
}
case terms.Dict: {
const children = getAllChildren(node).filter((n) => n.type.id !== terms.Comment)
const children = getAllChildren(node)
const instructions: ProgramItem[] = []
children.forEach((node) => {

View File

@ -22,7 +22,8 @@ export const getAllChildren = (node: SyntaxNode): SyntaxNode[] => {
children.push(child)
child = child.nextSibling
}
return children
return children.filter((n) => n.type.id !== terms.Comment)
}
export const getBinaryParts = (node: SyntaxNode) => {
@ -50,13 +51,15 @@ export const getAssignmentParts = (node: SyntaxNode) => {
// array destructuring
if (left && left.type.id === terms.Array) {
const identifiers = getAllChildren(left).filter(child => child.type.id === terms.Identifier)
const identifiers = getAllChildren(left).filter((child) => child.type.id === terms.Identifier)
return { arrayPattern: identifiers, right }
}
if (!left || left.type.id !== terms.AssignableIdentifier) {
throw new CompilerError(
`Assign left child must be an AssignableIdentifier or Array, got ${left ? left.type.name : 'none'}`,
`Assign left child must be an AssignableIdentifier or Array, got ${
left ? left.type.name : 'none'
}`,
node.from,
node.to
)
@ -71,7 +74,9 @@ export const getCompoundAssignmentParts = (node: SyntaxNode) => {
if (!left || left.type.id !== terms.AssignableIdentifier) {
throw new CompilerError(
`CompoundAssign left child must be an AssignableIdentifier, got ${left ? left.type.name : 'none'}`,
`CompoundAssign left child must be an AssignableIdentifier, got ${
left ? left.type.name : 'none'
}`,
node.from,
node.to
)