Class: LivetextAST
Overview
Livetext AST Parser for Inline Formatting, Variables, and Functions Parses Livetext syntax into s-expression style Ruby arrays
Features:
-
Inline formatting: *bold, _italic, ‘code, ~strike, **double, *[bracketed]
-
Variables: $name, $my_var, $font.title (with validation)
-
Functions: $$func, $$func:param, $$func, $$func (space/eol)
-
Escaped characters: *, _, `, ~, $, $$
-
Parameter delimiter tracking for functions (:space, :eol, :colon, :lbrack)
Edge cases handled to match Livetext behavior:
-
$foo. -> parses $foo as variable, . as literal
-
$a..b -> parses $a as variable, ..b as literal
-
Invalid names left as literal text
Constant Summary collapse
- TEXT =
AST node type constants
:text
- BOLD =
:bold
- ITALIC =
:italic
- CODE =
:code
- STRIKE =
:strike
- VAR =
:var
- FUNC =
:func
- BODY =
:body
- DIRECTIVE =
:directive
- ERROR =
:error
- SPACE =
Function parameter delimiter constants
:space
- EOL =
:eol
- COLON =
:colon
- LBRACK =
:lbrack
Instance Method Summary collapse
-
#initialize ⇒ LivetextAST
constructor
A new instance of LivetextAST.
- #parse_directives(lines) ⇒ Object
- #parse_functions(text) ⇒ Object
- #parse_inline_formatting(text) ⇒ Object
- #parse_variables(text) ⇒ Object
Constructor Details
#initialize ⇒ LivetextAST
Returns a new instance of LivetextAST.
34 35 36 37 |
# File 'lib/livetext/ast.rb', line 34 def initialize @escaped_chars = {} @escape_counter = 0 end |
Instance Method Details
#parse_directives(lines) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/livetext/ast.rb', line 89 def parse_directives(lines) return [] if lines.nil? || lines.empty? # Step 2: Parse each directive result = [] i = 0 while i < lines.length line = lines[i] if line.start_with?('.') && line.strip != ".end" directive_result = parse_single_directive(lines, i) if directive_result.is_a?(Array) && directive_result.first == ERROR # Error occurred, return the error return directive_result else result << directive_result # Skip to the line after the directive (including body if any) if directive_result.is_a?(Array) && directive_result.first == DIRECTIVE && directive_result.length == 4 # Multi-line directive with body, find the .end j = i + 1 while j < lines.length && lines[j].strip != ".end" j += 1 end i = j # Skip to after .end end end end i += 1 end # Step 3: Return single directive or array of directives result.length == 1 ? result.first : result end |
#parse_functions(text) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/livetext/ast.rb', line 73 def parse_functions(text) return [] if text.nil? || text.empty? # Step 1: Mark escaped dollar signs text = mark_escaped_dollars(text) # Step 2: Parse functions result = parse_function_markers(text) # Step 3: Restore escaped characters result = restore_escaped_characters(result) # Step 4: Convert to s-expression format convert_to_function_sexpr(result) end |
#parse_inline_formatting(text) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/livetext/ast.rb', line 39 def parse_inline_formatting(text) return [] if text.nil? || text.empty? # Step 1: Mark escaped characters text = mark_escaped_characters(text) # Step 2: Parse in order of precedence result = parse_bracketed_markers(text) result = parse_double_markers(result) result = parse_single_markers(result) # Step 3: Restore escaped characters result = restore_escaped_characters(result) # Step 4: Convert to s-expression format convert_to_formatting_sexpr(result) end |
#parse_variables(text) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/livetext/ast.rb', line 57 def parse_variables(text) return [] if text.nil? || text.empty? # Step 1: Mark escaped dollar signs text = mark_escaped_dollars(text) # Step 2: Parse variables result = parse_variable_markers(text) # Step 3: Restore escaped characters result = restore_escaped_characters(result) # Step 4: Convert to s-expression format convert_to_variable_sexpr(result) end |