Class: Kward::NativeShell::Lexer
- Inherits:
-
Object
- Object
- Kward::NativeShell::Lexer
- Defined in:
- lib/kward/shell/native_syntax.rb
Constant Summary collapse
- OPERATORS =
%w[&& || <<- << >> <> >| >& <& ;; ;& ; & | > < ( ) { }].freeze
Instance Method Summary collapse
- #heredoc_word ⇒ Object
-
#initialize(source, depth: 0, aliases: {}, line: 1, eof: true) ⇒ Lexer
constructor
A new instance of Lexer.
- #next_token ⇒ Object
- #queue_heredoc(document) ⇒ Object
- #regex_token ⇒ Object
-
#replace_alias(token, source, names) ⇒ Object
Reinsert alias text into the same lexical stream.
- #tokens ⇒ Object
Constructor Details
#initialize(source, depth: 0, aliases: {}, line: 1, eof: true) ⇒ Lexer
Returns a new instance of Lexer.
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/kward/shell/native_syntax.rb', line 63 def initialize(source, depth: 0, aliases: {}, line: 1, eof: true) @source = source.dup @eof = eof @alias_regions = [] @token_end = 0 @aliases = aliases @substitution_depth = depth @index = 0 @line = line @arithmetic_depth = 0 @parameter_depth = 0 @heredocs = [] end |
Instance Method Details
#heredoc_word ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/kward/shell/native_syntax.rb', line 163 def heredoc_word parts = [] until current.nil? case current when "$" then parameter(parts, true) when "`" then backticks(parts, true) when "\\" advance if ['$', '`', "\\", "\n"].include?(current) literal(parts, current, true) unless current == "\n" advance else literal(parts, "\\", true) end else literal(parts, current, true) advance end end Word.new(parts: parts) end |
#next_token ⇒ Object
85 86 87 88 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 121 122 123 124 |
# File 'lib/kward/shell/native_syntax.rb', line 85 def next_token until current.nil? line = @line start = @index case current when " ", "\t", "\r" advance when "#" advance until current.nil? || current == "\n" when "\n" advance consume_heredocs return token(";", line: line, start: start) else if @source[@index, 2] == "\\\n" advance(2) raise IncompleteInput, "continued input line" if current.nil? && !@eof next end operator = OPERATORS.find { |candidate| @source[@index, candidate.length] == candidate } operator = nil if ["{", "}"].include?(operator) && @source[@index + 1]&.match?(/[^\s;|&()<>]/) value = if @source[@index, 2] == "((" [:arithmetic, arithmetic_source] elsif ["<(", ">("].include?(@source[@index, 2]) word elsif (match = @source[@index..].match(/\A([0-9]+)(<<-|<<|>>|<>|>\||>&|<&|>|<)/)) advance(match[0].length) [:redirect, match[1].to_i, match[2]] elsif operator advance(operator.length) operator else word end return token(value, line: line, start: start, source: @source[start...@index]) end end raise IncompleteInput, "unclosed here-document" unless @heredocs.empty? nil end |
#queue_heredoc(document) ⇒ Object
159 160 161 |
# File 'lib/kward/shell/native_syntax.rb', line 159 def queue_heredoc(document) @heredocs << document end |
#regex_token ⇒ Object
126 127 128 129 130 131 132 |
# File 'lib/kward/shell/native_syntax.rb', line 126 def regex_token advance while current&.match?(/\s/) return unless current line, start = @line, @index value = word(regex: true) token(value, line: line, start: start, source: @source[start...@index]) end |
#replace_alias(token, source, names) ⇒ Object
Reinsert alias text into the same lexical stream. Quotes and here-docs can cross the replacement boundary, unlike independently lexed tokens.
136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/kward/shell/native_syntax.rb', line 136 def replace_alias(token, source, names) first, last = token.start, token.finish delta = source.length - (last - first) @alias_regions.each do |region| region[:start] += delta if region[:start] >= last region[:finish] += delta if region[:finish] >= last end @source[first...last] = source @alias_regions << { start: first, finish: first + source.length, names: names, trailing: source.end_with?(" ", "\t") } @index = @token_end = first @line = token.line end |
#tokens ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/kward/shell/native_syntax.rb', line 77 def tokens result = [] while (token = next_token) result << token end result end |