import { BlankNode } from "../Nodes/BlankNode" ; import { CommandNode } from "../Nodes/CommandNode" ; import { IBudgieNode } from "../Nodes/IBudgieNode" ; import { TextNode } from "../Nodes/TextNode" ; import { TextParsing } from "./TextParsing" ; /**
 * Parses individual lines of raw syntax into Budgie nodes.
 */ export class SourceLineParser { param rawLine public parseLine ( rawLine ) { rawLine = rawLine . trim ( ) ; if ( rawLine === "" ) { return new BlankNode ( ) ; } const colonIndex = rawLine . indexOf ( ":" ) ; if ( colonIndex === - 1 ) { return new CommandNode ( rawLine . trim ( ) , [ ] ) ; } const command = rawLine . substring ( 0 , colonIndex ) . trim ( ) ; const args = [ ] ; this . parseCommandArgs ( rawLine , colonIndex + 1 , args ) ; return new CommandNode ( command , args ) ; } param rawLine param start param nodes param withinParenthesis returns private parseCommandArgs ( rawLine , start , nodes ) { for ( let i = start ; i < rawLine . length ; i += 1 ) { if ( rawLine [ i ] === "{" ) { i = this . parseSubCommand ( rawLine , i , nodes ) ; continue ; } if ( rawLine [ i ] === "}" ) { return i + 1 ; } if ( rawLine [ i ] === "(" ) { i = this . parseParenthesis ( rawLine , i + 1 , nodes ) ; continue ; } if ( rawLine [ i ] === " " ) { continue ; } i = this . parseTextCommand ( rawLine , i , nodes ) ; } return rawLine . length ; } param rawLine param start param nodes returns private parseSubCommand ( rawLine , i , nodes ) { i = TextParsing . getNextStartOfWordIndex ( rawLine , i + 1 ) ; const commandNameEnd = TextParsing . getNextEndOfCommandNameIndex ( rawLine , i ) ; const commandNameRaw = rawLine . substring ( i , commandNameEnd ) ; const commandName = commandNameRaw . trim ( ) ; i = TextParsing . getNextNonSpaceIndex ( rawLine , commandNameEnd ) ; if ( rawLine [ i ] === "}" ) { nodes . push ( new CommandNode ( commandName , [ ] ) ) ; return i + 1 ; } const commandArgs = [ ] ; i = this . parseCommandArgs ( rawLine , i + 1 , commandArgs ) ; nodes . push ( new CommandNode ( commandName , commandArgs ) ) ; return i ; } param rawLine param start param nodes returns private parseParenthesis ( rawLine , i , nodes ) { const nextEndOfWordIndex = TextParsing . getNextEndOfParenthesisWordIndex ( rawLine , i ) ; const textRaw = rawLine . substring ( i , nextEndOfWordIndex ) ; const text = TextParsing . removeBackslashesFromWord ( textRaw ) ; nodes . push ( new TextNode ( text ) ) ; return nextEndOfWordIndex ; } param rawLine param start param nodes returns private parseTextCommand ( rawArgs , i , nodes ) { const nextEndOfWordIndex = TextParsing . getNextEndOfWordIndex ( rawArgs , i ) ; const text = rawArgs . substring ( i , nextEndOfWordIndex ) ; nodes . push ( new TextNode ( text ) ) ; return nextEndOfWordIndex ; } }