Class: ShellOpts::Lexer
- Inherits:
-
Object
- Object
- ShellOpts::Lexer
- Defined in:
- lib/shellopts/lexer.rb
Constant Summary collapse
- COMMAND_RE =
/[a-z][a-z._-]*!/
- DECL_RE =
/^(?:-|--|\+|\+\+|(?:@(?:\s|$))|(?:[^\\!]\S*!(?:\s|$)))/
- SPEC_RE =
Match ArgSpec argument words. TODO
/^[^a-z]{2,}$/
- DESCR_RE =
Match ArgDescr words (should be at least two characters long)
/^[^a-z]{2,}$/
- SECTIONS =
%w(DESCRIPTION OPTION OPTIONS COMMAND COMMANDS)
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Name of program.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(name, source, oneline) ⇒ Lexer
constructor
A new instance of Lexer.
- #lex(lineno = 1, charno = 1) ⇒ Object
- #lexer_error(token, message) ⇒ Object
- #oneline? ⇒ Boolean
Constructor Details
#initialize(name, source, oneline) ⇒ Lexer
Returns a new instance of Lexer.
58 59 60 61 62 63 |
# File 'lib/shellopts/lexer.rb', line 58 def initialize(name, source, oneline) @name = name @source = source @oneline = oneline @source += "\n" if @source[-1] != "\n" # Always terminate source with a newline end |
Instance Attribute Details
#name ⇒ Object (readonly)
Name of program
52 53 54 |
# File 'lib/shellopts/lexer.rb', line 52 def name @name end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
53 54 55 |
# File 'lib/shellopts/lexer.rb', line 53 def source @source end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
54 55 56 |
# File 'lib/shellopts/lexer.rb', line 54 def tokens @tokens end |
Class Method Details
Instance Method Details
#lex(lineno = 1, charno = 1) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/shellopts/lexer.rb', line 65 def lex(lineno = 1, charno = 1) # Split source into lines and tag them with lineno and charno. Only the # first line can have charno != 1 lines = source[0..-2].split("\n").map.with_index { |line,i| l = Line.new(i + lineno, charno, line) charno = 1 l } # Skip initial comments and blank lines and compute indent level lines.shift_while { |line| line.text == "" || line.text.start_with?("#") && line.charno == 1 } initial_indent = lines.first&.charno # Create program token. The source is the program name @tokens = [Token.new(:program, 0, 0, name)] # Used to detect code blocks last_nonblank = @tokens.first # Process lines while line = lines.shift # Pass-trough blank lines if line.to_s == "" @tokens << Token.new(:blank, line.lineno, line.charno, "") next end # Ignore meta comments if line.charno < initial_indent next if line =~ /^#/ error_token = Token.new(:text, line.lineno, 0, "") lexer_error error_token, "Illegal indentation" end # Line without escape sequences source = line.text[(line.text =~ /^\\/ ? 1 : 0)..-1] # Code lines if last_nonblank.kind == :text && line.charno > last_nonblank.charno && line !~ DECL_RE @tokens << Token.new(:text, line.lineno, line.charno, source) lines.shift_while { |line| line.blank? || line.charno > last_nonblank.charno }.each { |line| kind = (line.blank? ? :blank : :text) @tokens << Token.new(kind, line.lineno, line.charno, line.text) } # Sections elsif SECTIONS.include?(line.text) @tokens << Token.new(:section, line.lineno, line.charno, line.text.sub(/S$/, "")) # Options, commands, usage, arguments, and briefs elsif line =~ DECL_RE words = line.words while (charno, word = words.shift) case word when "@" if words.empty? error_token = Token.new(:text, line.lineno, charno, "@") lexer_error error_token, "Empty '@' declaration" end source = words.shift_while { true }.map(&:last).join(" ") @tokens << Token.new(:brief, line.lineno, charno, source) when "--" # FIXME Rename argdescr @tokens << Token.new(:usage, line.lineno, charno, "--") source = words.shift_while { |_,w| w =~ DESCR_RE }.map(&:last).join(" ") @tokens << Token.new(:usage_string, line.lineno, charno, source) when "++" # FIXME Rename argspec @tokens << Token.new(:spec, line.lineno, charno, "++") words.shift_while { |c,w| w =~ SPEC_RE and @tokens << Token.new(:argument, line.lineno, c, w) } when /^-|\+/ @tokens << Token.new(:option, line.lineno, charno, word) when /!$/ @tokens << Token.new(:command, line.lineno, charno, word) else source = [word, words.shift_while { |_,w| w !~ DECL_RE }.map(&:last)].flatten.join(" ") @tokens << Token.new(:brief, line.lineno, charno, source) end end # TODO: Move to parser and remove @oneline from Lexer (token = @tokens.last).kind != :brief || !oneline? or lexer_error token, "Briefs are only allowed in multi-line specifications" # Paragraph lines else @tokens << Token.new(:text, line.lineno, line.charno, source) end # FIXME Not sure about this # last_nonblank = @tokens.last last_nonblank = @tokens.last if ![:blank, :usage_string, :argument].include? @tokens.last.kind end # Move arguments and briefs before first command if one-line source # if oneline? && cmd_index = @tokens.index { |token| token.kind == :command } # @tokens = # @tokens[0...cmd_index] + # @tokens[cmd_index..-1].partition { |token| ![:command, :option].include?(token.kind) }.flatten # end @tokens end |
#lexer_error(token, message) ⇒ Object
172 |
# File 'lib/shellopts/lexer.rb', line 172 def lexer_error(token, ) raise LexerError.new(token), end |
#oneline? ⇒ Boolean
56 |
# File 'lib/shellopts/lexer.rb', line 56 def oneline?() @oneline end |