Class: CodeRay::Scanners::Bash
- Inherits:
-
Scanner
- Object
- Scanner
- CodeRay::Scanners::Bash
- Defined in:
- lib/coderay/scanners/bash.rb
Constant Summary collapse
- RESERVED_WORDS =
%w( ! [[ ]] case do done elif else esac fi for function if in select then time until while { } )
- COMMANDS =
%w( : . break cd continue eval exec exit export getopts hash pwd readonly return shift test [ ] times trap umask unset )
- BASH_COMMANDS =
%w( alias bind builtin caller command declare echo enable help let local logout printf read set shopt source type typeset ulimit unalias )
- VARIABLES =
%w( CDPATH HOME IFS MAIL MAILPATH OPTARG OPTIND PATH PS1 PS2 )
- BASH_VARIABLES =
%w( BASH BASH_ARGC BASH_ARGV BASH_COMMAND BASH_ENV BASH_EXECUTION_STRING BASH_LINENO BASH_REMATCH BASH_SOURCE BASH_SUBSHELL BASH_VERSINFO BASH_VERSINFO[0] BASH_VERSINFO[1] BASH_VERSINFO[2] BASH_VERSINFO[3] BASH_VERSINFO[4] BASH_VERSINFO[5] BASH_VERSION COLUMNS COMP_CWORD COMP_LINE COMP_POINT COMP_WORDBREAKS COMP_WORDS COMPREPLAY DIRSTACK EMACS EUID FCEDIT FIGNORE FUNCNAME GLOBIGNORE GROUPS histchars HISTCMD HISTCONTROL HISTFILE HISTFILESIZE HISTIGNORE HISTSIZE HISTTIMEFORMAT HOSTFILE HOSTNAME HOSTTYPE IGNOREEOF INPUTRC LANG LC_ALL LC_COLLATE LC_CTYPE LC_MESSAGE LC_NUMERIC LINENNO LINES MACHTYPE MAILCHECK OLDPWD OPTERR OSTYPE PIPESTATUS POSIXLY_CORRECT PPID PROMPT_COMMAND PS3 PS4 PWD RANDOM REPLAY SECONDS SHELL SHELLOPTS SHLVL TIMEFORMAT TMOUT TMPDIR UID )
- PRE_CONSTANTS =
/ \$\{? (?: \# | \? | \d | \* | @ | - | \$ | \! | _ ) \}? /ox
- IDENT_KIND =
WordList.new(:ident). add(RESERVED_WORDS, :reserved). add(COMMANDS, :method). add(BASH_COMMANDS, :method). add(VARIABLES, :pre_type). add(BASH_VARIABLES, :pre_type)
Instance Attribute Summary collapse
-
#quote ⇒ Object
readonly
Returns the value of attribute quote.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
- #handle_error(match, options) ⇒ Object
-
#initialize(*args) ⇒ Bash
constructor
A new instance of Bash.
- #scan_tokens(tokens, options) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Bash
Returns a new instance of Bash.
54 55 56 57 58 |
# File 'lib/coderay/scanners/bash.rb', line 54 def initialize(*args) super(*args) @state = :initial @quote = nil end |
Instance Attribute Details
#quote ⇒ Object (readonly)
Returns the value of attribute quote.
52 53 54 |
# File 'lib/coderay/scanners/bash.rb', line 52 def quote @quote end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
52 53 54 |
# File 'lib/coderay/scanners/bash.rb', line 52 def state @state end |
Instance Method Details
#handle_error(match, options) ⇒ Object
194 195 196 197 198 199 200 201 |
# File 'lib/coderay/scanners/bash.rb', line 194 def handle_error(match, ) o = {:ignore_errors => true}.merge() if o[:ignore_errors] [match, :plain] else [">>>>>#{match}<<<<<", :error] end end |
#scan_tokens(tokens, options) ⇒ Object
60 61 62 63 64 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/coderay/scanners/bash.rb', line 60 def scan_tokens tokens, until eos? kind = match = nil if match = scan(/\n/) tokens << [match, :end_line] next end if @state == :initial if match = scan(/\A#!.*/) kind = :directive elsif match = scan(/#.*/) kind = :comment elsif match = scan(/(?:\. |source ).*/) kind = :reserved elsif match = scan(/(?:\\.|,)/) kind = :plain elsif match = scan(/;/) kind = :delimiter elsif match = scan(/(?:"|`)/) @state = :quote @quote = match tokens << [:open, :string] if @quote == '"' tokens << [:open, :shell] if @quote == '`' tokens << [match, :delimiter] next elsif match = scan(/'[^']*'/) kind = :string elsif match = scan(/(?: \& | > | < | \| >> | << | >\& )/ox) kind = :bin elsif match = scan(/\d+[\.-](?:\d+[\.-]?)+/) #versions, dates, and hyphen delimited numbers kind = :float elsif match = scan(/\d+\.\d+\s+/) kind = :float elsif match = scan(/\d+/) kind = :integer elsif match = scan(/ (?: \$\(\( | \)\) ) /x) kind = :global_variable elsif match = scan(/ \$\{ [^\}]+ \} /ox) match =~ /\$\{(.*)\}/ kind = IDENT_KIND[$1] kind = :instance_variable if kind == :ident elsif match = scan(/ \$\( [^\)]+ \) /ox) kind = :shell elsif match = scan(PRE_CONSTANTS) kind = :pre_constant elsif match = scan(/[^\s'"]*[A-Za-z_][A-Za-z_0-9]*\+?=/) match =~ /(.*?)([A-Za-z_][A-Za-z_0-9]*)(\+?=)/ str = $1 pre = $2 op = $3 kind = :plain if str.to_s.strip.empty? kind = IDENT_KIND[pre] kind = :instance_variable if kind == :ident tokens << [pre, kind] tokens << [op, :operator] next end elsif match = scan(/[A-Za-z_]+\[[A-Za-z_\d]+\]/) # array kind = IDENT_KIND(match) kind = :instance_variable if kind == :ident elsif match = scan(/ \$[A-Za-z_][A-Za-z_0-9]* /ox) match =~ /\$(.*)/ kind = IDENT_KIND[$1] kind = :instance_variable if kind == :ident elsif match = scan(/read \S+/) match =~ /read(\s+)(\S+)/ tokens << ['read', :method] tokens << [$1, :space] tokens << [$2, :instance_variable] next elsif match = scan(/[\!\:\[\]\{\}]/) kind = :reserved elsif match = scan(/ [A-Za-z_][A-Za-z_\d]*;? /x) match =~ /([^;]+);?/ kind = IDENT_KIND[$1] if match[/([^;]+);$/] tokens << [$1, kind] tokens << [';', :delimiter] next end elsif match = scan(/(?: = | - | \+ | \{ | \} | \( | \) | && | \|\| | ;; | ! )/ox) kind = :operator elsif match = scan(/\s+/) kind = :space elsif match = scan(/[^ \$"'`\d]/) kind = :plain elsif match = scan(/.+/) # this shouldn't be :reserved for highlighting bad matches match, kind = handle_error(match, ) end elsif @state == :quote if (match = scan(/\\.?/)) kind = :content elsif match = scan(/#{@quote}/) tokens << [match, :delimiter] tokens << [:close, :string] if @quote == '"' tokens << [:close, :shell] if @quote == "`" @quote = nil @state = :initial next #kind = :symbol elsif match = scan(PRE_CONSTANTS) kind = :pre_constant elsif match = scan(/ \$\{?[A-Za-z_][A-Za-z_\d]*\}? /x) kind = IDENT_KIND[match] kind = :instance_variable if kind == :ident elsif (@quote == '`') and (match = scan(/\$"/)) kind = :content elsif (@quote == '"') and (match = scan(/\$`/)) kind = :content elsif match = scan(/[^#{@quote}\$\\]+/) kind = :content else match = scan(/.+/) # this shouldn't be #kind = :reserved #raise match match, kind = handle_error(match, ) end end match ||= matched tokens << [match, kind] end tokens end |