llm-shell 0.9.2 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +61 -66
- data/lib/llm/shell/command.rb +40 -40
- data/lib/llm/shell/commands/clear_screen.rb +4 -18
- data/lib/llm/shell/commands/debug_mode.rb +12 -0
- data/lib/llm/shell/commands/dir_import.rb +4 -20
- data/lib/llm/shell/commands/disable_tool.rb +33 -0
- data/lib/llm/shell/commands/enable_tool.rb +33 -0
- data/lib/llm/shell/commands/file_import.rb +4 -20
- data/lib/llm/shell/commands/help.rb +23 -36
- data/lib/llm/shell/commands/show_chat.rb +4 -19
- data/lib/llm/shell/commands/show_version.rb +4 -20
- data/lib/llm/shell/commands/system_prompt.rb +4 -18
- data/lib/llm/shell/completion.rb +5 -5
- data/lib/llm/shell/config.rb +4 -5
- data/lib/llm/shell/formatter.rb +1 -2
- data/lib/llm/shell/internal/coderay/lib/coderay/duo.rb +81 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/_map.rb +17 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/comment_filter.rb +25 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/count.rb +39 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/debug.rb +49 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/debug_lint.rb +63 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/div.rb +23 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/encoder.rb +190 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/filter.rb +58 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/html/css.rb +65 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/html/numbering.rb +108 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/html/output.rb +164 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/html.rb +333 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/json.rb +83 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/lines_of_code.rb +45 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/lint.rb +59 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/null.rb +18 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/page.rb +24 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/span.rb +23 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/statistic.rb +95 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/terminal.rb +195 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/text.rb +46 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/token_kind_filter.rb +111 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/xml.rb +72 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/yaml.rb +50 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders.rb +18 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/for_redcloth.rb +95 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/helpers/file_type.rb +151 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/helpers/plugin.rb +55 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/helpers/plugin_host.rb +221 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/helpers/word_list.rb +72 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/_map.rb +24 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/c.rb +189 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/clojure.rb +217 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/cpp.rb +217 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/css.rb +196 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/debug.rb +75 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/delphi.rb +144 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/diff.rb +221 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/erb.rb +81 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/go.rb +208 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/groovy.rb +268 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/haml.rb +168 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/html.rb +275 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/java/builtin_types.rb +421 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/java.rb +174 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/java_script.rb +236 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/json.rb +98 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/lua.rb +280 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/php.rb +527 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/python.rb +287 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/raydebug.rb +75 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/ruby/patterns.rb +178 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/ruby/string_state.rb +79 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/ruby.rb +477 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/sass.rb +232 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/scanner.rb +337 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/sql.rb +169 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/taskpaper.rb +36 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/text.rb +26 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/xml.rb +17 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/yaml.rb +140 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners.rb +27 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/styles/_map.rb +7 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/styles/alpha.rb +153 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/styles/style.rb +18 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/styles.rb +15 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/token_kinds.rb +85 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/tokens.rb +164 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/tokens_proxy.rb +55 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/version.rb +3 -0
- data/lib/llm/shell/internal/coderay/lib/coderay.rb +284 -0
- data/lib/llm/shell/internal/io-line/lib/io/line/multiple.rb +19 -0
- data/lib/{io → llm/shell/internal/io-line/lib/io}/line.rb +2 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/bot/builder.rb +31 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/bot/conversable.rb +37 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/bot/prompt/completion.rb +49 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/bot/prompt/respond.rb +49 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/bot.rb +150 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/buffer.rb +162 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/client.rb +36 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/error.rb +49 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/eventhandler.rb +44 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/eventstream/event.rb +69 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/eventstream/parser.rb +88 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/eventstream.rb +8 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/file.rb +91 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/function.rb +177 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/message.rb +178 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/mime.rb +140 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/multipart.rb +101 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/object/builder.rb +38 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/object/kernel.rb +53 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/object.rb +89 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/provider.rb +352 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/error_handler.rb +36 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/files.rb +155 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/format/completion_format.rb +88 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/format.rb +29 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/models.rb +54 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/response/completion.rb +39 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/response/enumerable.rb +11 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/response/file.rb +23 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/response/web_search.rb +21 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/stream_parser.rb +66 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic.rb +138 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/deepseek/format/completion_format.rb +68 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/deepseek/format.rb +27 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/deepseek.rb +75 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/audio.rb +73 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/error_handler.rb +47 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/files.rb +146 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/format/completion_format.rb +69 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/format.rb +39 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/images.rb +133 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/models.rb +60 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/completion.rb +35 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/embedding.rb +8 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/file.rb +11 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/files.rb +15 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/image.rb +31 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/models.rb +15 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/web_search.rb +22 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/stream_parser.rb +86 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini.rb +173 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/llamacpp.rb +74 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/error_handler.rb +36 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/format/completion_format.rb +77 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/format.rb +29 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/models.rb +56 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/response/completion.rb +28 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/response/embedding.rb +9 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/stream_parser.rb +44 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama.rb +116 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/audio.rb +91 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/error_handler.rb +46 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/files.rb +134 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/format/completion_format.rb +90 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/format/moderation_format.rb +35 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/format/respond_format.rb +72 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/format.rb +54 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/images.rb +109 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/models.rb +55 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/moderations.rb +65 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/audio.rb +7 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/completion.rb +40 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/embedding.rb +9 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/enumerable.rb +23 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/file.rb +7 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/image.rb +16 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/moderations.rb +34 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/responds.rb +48 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/web_search.rb +21 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/responses/stream_parser.rb +76 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/responses.rb +99 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/stream_parser.rb +86 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/vector_stores.rb +228 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai.rb +206 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/xai/images.rb +58 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/xai.rb +72 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/zai.rb +74 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/response.rb +67 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/array.rb +26 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/boolean.rb +13 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/integer.rb +43 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/leaf.rb +78 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/null.rb +13 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/number.rb +43 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/object.rb +41 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/string.rb +34 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/version.rb +8 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema.rb +81 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/server_tool.rb +32 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/tool/param.rb +75 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/tool.rb +78 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/utils.rb +19 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/version.rb +5 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm.rb +121 -0
- data/lib/llm/shell/internal/optparse/lib/optionparser.rb +2 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/ac.rb +70 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/date.rb +18 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/kwargs.rb +27 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/shellwords.rb +7 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/time.rb +11 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/uri.rb +7 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/version.rb +80 -0
- data/lib/llm/shell/internal/optparse/lib/optparse.rb +2469 -0
- data/lib/llm/shell/internal/paint/lib/paint/constants.rb +104 -0
- data/lib/llm/shell/internal/paint/lib/paint/pa.rb +13 -0
- data/lib/llm/shell/internal/paint/lib/paint/rgb_colors.rb +14 -0
- data/lib/llm/shell/internal/paint/lib/paint/shortcuts.rb +100 -0
- data/lib/llm/shell/internal/paint/lib/paint/shortcuts_version.rb +5 -0
- data/lib/llm/shell/internal/paint/lib/paint/util.rb +16 -0
- data/lib/llm/shell/internal/paint/lib/paint/version.rb +5 -0
- data/lib/llm/shell/internal/paint/lib/paint.rb +261 -0
- data/lib/llm/shell/internal/reline/lib/reline/config.rb +378 -0
- data/lib/llm/shell/internal/reline/lib/reline/face.rb +199 -0
- data/lib/llm/shell/internal/reline/lib/reline/history.rb +76 -0
- data/lib/llm/shell/internal/reline/lib/reline/io/ansi.rb +322 -0
- data/lib/llm/shell/internal/reline/lib/reline/io/dumb.rb +120 -0
- data/lib/llm/shell/internal/reline/lib/reline/io/windows.rb +530 -0
- data/lib/llm/shell/internal/reline/lib/reline/io.rb +55 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor/base.rb +37 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor/composite.rb +17 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor/emacs.rb +517 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor/vi_command.rb +518 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor/vi_insert.rb +517 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor.rb +8 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_stroke.rb +119 -0
- data/lib/llm/shell/internal/reline/lib/reline/kill_ring.rb +125 -0
- data/lib/llm/shell/internal/reline/lib/reline/line_editor.rb +2356 -0
- data/lib/llm/shell/internal/reline/lib/reline/unicode/east_asian_width.rb +1292 -0
- data/lib/llm/shell/internal/reline/lib/reline/unicode.rb +421 -0
- data/lib/llm/shell/internal/reline/lib/reline/version.rb +3 -0
- data/lib/llm/shell/internal/reline/lib/reline.rb +527 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/generated_parser.rb +712 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/handler.rb +268 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/local_date.rb +35 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/local_date_time.rb +42 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/local_time.rb +40 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/parser.rb +21 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/scanner.rb +92 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/string_utils.rb +40 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/version.rb +5 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb.rb +49 -0
- data/lib/llm/shell/options.rb +1 -1
- data/lib/llm/shell/renderer.rb +2 -3
- data/lib/llm/shell/repl.rb +21 -16
- data/lib/llm/shell/tool.rb +42 -0
- data/lib/llm/shell/tools/read_file.rb +15 -0
- data/lib/llm/shell/tools/system.rb +17 -0
- data/lib/llm/shell/tools/write_file.rb +16 -0
- data/lib/llm/shell/version.rb +1 -1
- data/lib/llm/shell.rb +83 -39
- data/libexec/llm-shell/shell +4 -6
- data/llm-shell.gemspec +0 -4
- metadata +233 -63
- data/lib/llm/function.rb +0 -17
- data/lib/llm/shell/command/extension.rb +0 -42
- data/lib/llm/shell/commands/utils.rb +0 -21
- data/lib/llm/shell/functions/read_file.rb +0 -22
- data/lib/llm/shell/functions/write_file.rb +0 -22
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
module Scanners
|
|
3
|
+
|
|
4
|
+
# Scanner for C++.
|
|
5
|
+
#
|
|
6
|
+
# Aliases: +cplusplus+, c++
|
|
7
|
+
class CPlusPlus < Scanner
|
|
8
|
+
|
|
9
|
+
register_for :cpp
|
|
10
|
+
file_extension 'cpp'
|
|
11
|
+
title 'C++'
|
|
12
|
+
|
|
13
|
+
#-- http://www.cppreference.com/wiki/keywords/start
|
|
14
|
+
KEYWORDS = [
|
|
15
|
+
'and', 'and_eq', 'asm', 'bitand', 'bitor', 'break',
|
|
16
|
+
'case', 'catch', 'class', 'compl', 'const_cast',
|
|
17
|
+
'continue', 'default', 'delete', 'do', 'dynamic_cast', 'else',
|
|
18
|
+
'enum', 'export', 'for', 'goto', 'if', 'namespace', 'new',
|
|
19
|
+
'not', 'not_eq', 'or', 'or_eq', 'reinterpret_cast', 'return',
|
|
20
|
+
'sizeof', 'static_assert', 'static_cast', 'struct', 'switch',
|
|
21
|
+
'template', 'throw', 'try', 'typedef', 'typeid', 'typename', 'union',
|
|
22
|
+
'while', 'xor', 'xor_eq',
|
|
23
|
+
] # :nodoc:
|
|
24
|
+
|
|
25
|
+
PREDEFINED_TYPES = [
|
|
26
|
+
'bool', 'char', 'char16_t', 'char32_t', 'double', 'float',
|
|
27
|
+
'int', 'long', 'short', 'signed', 'unsigned',
|
|
28
|
+
'wchar_t', 'string',
|
|
29
|
+
] # :nodoc:
|
|
30
|
+
PREDEFINED_CONSTANTS = [
|
|
31
|
+
'false', 'true',
|
|
32
|
+
'EOF', 'NULL', 'nullptr'
|
|
33
|
+
] # :nodoc:
|
|
34
|
+
PREDEFINED_VARIABLES = [
|
|
35
|
+
'this',
|
|
36
|
+
] # :nodoc:
|
|
37
|
+
DIRECTIVES = [
|
|
38
|
+
'alignas', 'alignof', 'auto', 'const', 'constexpr', 'decltype', 'explicit',
|
|
39
|
+
'extern', 'final', 'friend', 'inline', 'mutable', 'noexcept', 'operator',
|
|
40
|
+
'override', 'private', 'protected', 'public', 'register', 'static',
|
|
41
|
+
'thread_local', 'using', 'virtual', 'void', 'volatile',
|
|
42
|
+
] # :nodoc:
|
|
43
|
+
|
|
44
|
+
IDENT_KIND = WordList.new(:ident).
|
|
45
|
+
add(KEYWORDS, :keyword).
|
|
46
|
+
add(PREDEFINED_TYPES, :predefined_type).
|
|
47
|
+
add(PREDEFINED_VARIABLES, :local_variable).
|
|
48
|
+
add(DIRECTIVES, :directive).
|
|
49
|
+
add(PREDEFINED_CONSTANTS, :predefined_constant) # :nodoc:
|
|
50
|
+
|
|
51
|
+
ESCAPE = / [rbfntv\n\\'"] | x[a-fA-F0-9]{1,2} | [0-7]{1,3} /x # :nodoc:
|
|
52
|
+
UNICODE_ESCAPE = / u[a-fA-F0-9]{4} | U[a-fA-F0-9]{8} /x # :nodoc:
|
|
53
|
+
|
|
54
|
+
protected
|
|
55
|
+
|
|
56
|
+
def scan_tokens encoder, options
|
|
57
|
+
|
|
58
|
+
state = :initial
|
|
59
|
+
label_expected = true
|
|
60
|
+
case_expected = false
|
|
61
|
+
label_expected_before_preproc_line = nil
|
|
62
|
+
in_preproc_line = false
|
|
63
|
+
|
|
64
|
+
until eos?
|
|
65
|
+
|
|
66
|
+
case state
|
|
67
|
+
|
|
68
|
+
when :initial
|
|
69
|
+
|
|
70
|
+
if match = scan(/ \s+ | \\\n /x)
|
|
71
|
+
if in_preproc_line && match != "\\\n" && match.index(?\n)
|
|
72
|
+
in_preproc_line = false
|
|
73
|
+
label_expected = label_expected_before_preproc_line
|
|
74
|
+
end
|
|
75
|
+
encoder.text_token match, :space
|
|
76
|
+
|
|
77
|
+
elsif match = scan(%r! // [^\n\\]* (?: \\. [^\n\\]* )* | /\* (?: .*? \*/ | .* ) !mx)
|
|
78
|
+
encoder.text_token match, :comment
|
|
79
|
+
|
|
80
|
+
elsif match = scan(/ \# \s* if \s* 0 /x)
|
|
81
|
+
match << scan_until(/ ^\# (?:elif|else|endif) .*? $ | \z /xm) unless eos?
|
|
82
|
+
encoder.text_token match, :comment
|
|
83
|
+
|
|
84
|
+
elsif match = scan(/ [-+*=<>?:;,!&^|()\[\]{}~%]+ | \/=? | \.(?!\d) /x)
|
|
85
|
+
label_expected = match =~ /[;\{\}]/
|
|
86
|
+
if case_expected
|
|
87
|
+
label_expected = true if match == ':'
|
|
88
|
+
case_expected = false
|
|
89
|
+
end
|
|
90
|
+
encoder.text_token match, :operator
|
|
91
|
+
|
|
92
|
+
elsif match = scan(/ [A-Za-z_][A-Za-z_0-9]* /x)
|
|
93
|
+
kind = IDENT_KIND[match]
|
|
94
|
+
if kind == :ident && label_expected && !in_preproc_line && scan(/:(?!:)/)
|
|
95
|
+
kind = :label
|
|
96
|
+
match << matched
|
|
97
|
+
else
|
|
98
|
+
label_expected = false
|
|
99
|
+
if kind == :keyword
|
|
100
|
+
case match
|
|
101
|
+
when 'class'
|
|
102
|
+
state = :class_name_expected
|
|
103
|
+
when 'case', 'default'
|
|
104
|
+
case_expected = true
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
encoder.text_token match, kind
|
|
109
|
+
|
|
110
|
+
elsif match = scan(/\$/)
|
|
111
|
+
encoder.text_token match, :ident
|
|
112
|
+
|
|
113
|
+
elsif match = scan(/L?"/)
|
|
114
|
+
encoder.begin_group :string
|
|
115
|
+
if match[0] == ?L
|
|
116
|
+
encoder.text_token match, 'L', :modifier
|
|
117
|
+
match = '"'
|
|
118
|
+
end
|
|
119
|
+
state = :string
|
|
120
|
+
encoder.text_token match, :delimiter
|
|
121
|
+
|
|
122
|
+
elsif match = scan(/#[ \t]*(\w*)/)
|
|
123
|
+
encoder.text_token match, :preprocessor
|
|
124
|
+
in_preproc_line = true
|
|
125
|
+
label_expected_before_preproc_line = label_expected
|
|
126
|
+
state = :include_expected if self[1] == 'include'
|
|
127
|
+
|
|
128
|
+
elsif match = scan(/ L?' (?: [^\'\n\\] | \\ #{ESCAPE} )? '? /ox)
|
|
129
|
+
label_expected = false
|
|
130
|
+
encoder.text_token match, :char
|
|
131
|
+
|
|
132
|
+
elsif match = scan(/0[xX][0-9A-Fa-f]+/)
|
|
133
|
+
label_expected = false
|
|
134
|
+
encoder.text_token match, :hex
|
|
135
|
+
|
|
136
|
+
elsif match = scan(/(?:0[0-7]+)(?![89.eEfF])/)
|
|
137
|
+
label_expected = false
|
|
138
|
+
encoder.text_token match, :octal
|
|
139
|
+
|
|
140
|
+
elsif match = scan(/(?:\d+)(?![.eEfF])L?L?/)
|
|
141
|
+
label_expected = false
|
|
142
|
+
encoder.text_token match, :integer
|
|
143
|
+
|
|
144
|
+
elsif match = scan(/\d[fF]?|\d*\.\d+(?:[eE][+-]?\d+)?[fF]?|\d+[eE][+-]?\d+[fF]?/)
|
|
145
|
+
label_expected = false
|
|
146
|
+
encoder.text_token match, :float
|
|
147
|
+
|
|
148
|
+
else
|
|
149
|
+
encoder.text_token getch, :error
|
|
150
|
+
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
when :string
|
|
154
|
+
if match = scan(/[^\\"]+/)
|
|
155
|
+
encoder.text_token match, :content
|
|
156
|
+
elsif match = scan(/"/)
|
|
157
|
+
encoder.text_token match, :delimiter
|
|
158
|
+
encoder.end_group :string
|
|
159
|
+
state = :initial
|
|
160
|
+
label_expected = false
|
|
161
|
+
elsif match = scan(/ \\ (?: #{ESCAPE} | #{UNICODE_ESCAPE} ) /mox)
|
|
162
|
+
encoder.text_token match, :char
|
|
163
|
+
elsif match = scan(/ \\ | $ /x)
|
|
164
|
+
encoder.end_group :string
|
|
165
|
+
encoder.text_token match, :error unless match.empty?
|
|
166
|
+
state = :initial
|
|
167
|
+
label_expected = false
|
|
168
|
+
else
|
|
169
|
+
raise_inspect "else case \" reached; %p not handled." % peek(1), encoder
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
when :include_expected
|
|
173
|
+
if match = scan(/<[^>\n]+>?|"[^"\n\\]*(?:\\.[^"\n\\]*)*"?/)
|
|
174
|
+
encoder.text_token match, :include
|
|
175
|
+
state = :initial
|
|
176
|
+
|
|
177
|
+
elsif match = scan(/\s+/)
|
|
178
|
+
encoder.text_token match, :space
|
|
179
|
+
state = :initial if match.index ?\n
|
|
180
|
+
|
|
181
|
+
else
|
|
182
|
+
state = :initial
|
|
183
|
+
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
when :class_name_expected
|
|
187
|
+
if match = scan(/ [A-Za-z_][A-Za-z_0-9]* /x)
|
|
188
|
+
encoder.text_token match, :class
|
|
189
|
+
state = :initial
|
|
190
|
+
|
|
191
|
+
elsif match = scan(/\s+/)
|
|
192
|
+
encoder.text_token match, :space
|
|
193
|
+
|
|
194
|
+
else
|
|
195
|
+
encoder.text_token getch, :error
|
|
196
|
+
state = :initial
|
|
197
|
+
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
else
|
|
201
|
+
raise_inspect 'Unknown state', encoder
|
|
202
|
+
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
if state == :string
|
|
208
|
+
encoder.end_group :string
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
encoder
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
end
|
|
217
|
+
end
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
module Scanners
|
|
3
|
+
|
|
4
|
+
class CSS < Scanner
|
|
5
|
+
|
|
6
|
+
register_for :css
|
|
7
|
+
|
|
8
|
+
KINDS_NOT_LOC = [
|
|
9
|
+
:comment,
|
|
10
|
+
:class, :pseudo_class, :tag,
|
|
11
|
+
:id, :directive,
|
|
12
|
+
:key, :value, :operator, :color, :float, :string,
|
|
13
|
+
:error, :important, :type,
|
|
14
|
+
] # :nodoc:
|
|
15
|
+
|
|
16
|
+
module RE # :nodoc:
|
|
17
|
+
Hex = /[0-9a-fA-F]/
|
|
18
|
+
Unicode = /\\#{Hex}{1,6}\b/ # differs from standard because it allows uppercase hex too
|
|
19
|
+
Escape = /#{Unicode}|\\[^\n0-9a-fA-F]/
|
|
20
|
+
NMChar = /[-_a-zA-Z0-9]/
|
|
21
|
+
NMStart = /[_a-zA-Z]/
|
|
22
|
+
String1 = /"(?:[^\n\\"]+|\\\n|#{Escape})*"?/ # TODO: buggy regexp
|
|
23
|
+
String2 = /'(?:[^\n\\']+|\\\n|#{Escape})*'?/ # TODO: buggy regexp
|
|
24
|
+
String = /#{String1}|#{String2}/
|
|
25
|
+
|
|
26
|
+
HexColor = /#(?:#{Hex}{6}|#{Hex}{3})/
|
|
27
|
+
|
|
28
|
+
Num = /-?(?:[0-9]*\.[0-9]+|[0-9]+)n?/
|
|
29
|
+
Name = /#{NMChar}+/
|
|
30
|
+
Ident = /-?#{NMStart}#{NMChar}*/
|
|
31
|
+
AtKeyword = /@#{Ident}/
|
|
32
|
+
Percentage = /#{Num}%/
|
|
33
|
+
|
|
34
|
+
reldimensions = %w[em ex px]
|
|
35
|
+
absdimensions = %w[in cm mm pt pc]
|
|
36
|
+
Unit = Regexp.union(*(reldimensions + absdimensions + %w[s dpi dppx deg]))
|
|
37
|
+
|
|
38
|
+
Dimension = /#{Num}#{Unit}/
|
|
39
|
+
|
|
40
|
+
Function = /(?:url|alpha|attr|counters?)\((?:[^)\n]|\\\))*\)?/
|
|
41
|
+
|
|
42
|
+
Id = /(?!#{HexColor}\b(?!-))##{Name}/
|
|
43
|
+
Class = /\.#{Name}/
|
|
44
|
+
PseudoClass = /::?#{Ident}/
|
|
45
|
+
AttributeSelector = /\[[^\]]*\]?/
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
protected
|
|
49
|
+
|
|
50
|
+
def setup
|
|
51
|
+
@state = :initial
|
|
52
|
+
@value_expected = false
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def scan_tokens encoder, options
|
|
56
|
+
states = Array(options[:state] || @state).dup
|
|
57
|
+
value_expected = @value_expected
|
|
58
|
+
|
|
59
|
+
until eos?
|
|
60
|
+
|
|
61
|
+
if match = scan(/\s+/)
|
|
62
|
+
encoder.text_token match, :space
|
|
63
|
+
|
|
64
|
+
elsif case states.last
|
|
65
|
+
when :initial, :media
|
|
66
|
+
if match = scan(/(?>#{RE::Ident})(?!\()|\*/ox)
|
|
67
|
+
encoder.text_token match, :tag
|
|
68
|
+
next
|
|
69
|
+
elsif match = scan(RE::Class)
|
|
70
|
+
encoder.text_token match, :class
|
|
71
|
+
next
|
|
72
|
+
elsif match = scan(RE::Id)
|
|
73
|
+
encoder.text_token match, :id
|
|
74
|
+
next
|
|
75
|
+
elsif match = scan(RE::PseudoClass)
|
|
76
|
+
encoder.text_token match, :pseudo_class
|
|
77
|
+
next
|
|
78
|
+
elsif match = scan(RE::AttributeSelector)
|
|
79
|
+
# TODO: Improve highlighting inside of attribute selectors.
|
|
80
|
+
encoder.text_token match[0,1], :operator
|
|
81
|
+
encoder.text_token match[1..-2], :attribute_name if match.size > 2
|
|
82
|
+
encoder.text_token match[-1,1], :operator if match[-1] == ?]
|
|
83
|
+
next
|
|
84
|
+
elsif match = scan(/@media/)
|
|
85
|
+
encoder.text_token match, :directive
|
|
86
|
+
states.push :media_before_name
|
|
87
|
+
next
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
when :block
|
|
91
|
+
if match = scan(/(?>#{RE::Ident})(?!\()/ox)
|
|
92
|
+
if value_expected
|
|
93
|
+
encoder.text_token match, :value
|
|
94
|
+
else
|
|
95
|
+
encoder.text_token match, :key
|
|
96
|
+
end
|
|
97
|
+
next
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
when :media_before_name
|
|
101
|
+
if match = scan(RE::Ident)
|
|
102
|
+
encoder.text_token match, :type
|
|
103
|
+
states[-1] = :media_after_name
|
|
104
|
+
next
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
when :media_after_name
|
|
108
|
+
if match = scan(/\{/)
|
|
109
|
+
encoder.text_token match, :operator
|
|
110
|
+
states[-1] = :media
|
|
111
|
+
next
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
else
|
|
115
|
+
#:nocov:
|
|
116
|
+
raise_inspect 'Unknown state', encoder
|
|
117
|
+
#:nocov:
|
|
118
|
+
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
elsif match = scan(/\/\*(?:.*?\*\/|\z)/m)
|
|
122
|
+
encoder.text_token match, :comment
|
|
123
|
+
|
|
124
|
+
elsif match = scan(/\{/)
|
|
125
|
+
value_expected = false
|
|
126
|
+
encoder.text_token match, :operator
|
|
127
|
+
states.push :block
|
|
128
|
+
|
|
129
|
+
elsif match = scan(/\}/)
|
|
130
|
+
value_expected = false
|
|
131
|
+
encoder.text_token match, :operator
|
|
132
|
+
if states.last == :block || states.last == :media
|
|
133
|
+
states.pop
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
elsif match = scan(/#{RE::String}/o)
|
|
137
|
+
encoder.begin_group :string
|
|
138
|
+
encoder.text_token match[0, 1], :delimiter
|
|
139
|
+
encoder.text_token match[1..-2], :content if match.size > 2
|
|
140
|
+
encoder.text_token match[-1, 1], :delimiter if match.size >= 2
|
|
141
|
+
encoder.end_group :string
|
|
142
|
+
|
|
143
|
+
elsif match = scan(/#{RE::Function}/o)
|
|
144
|
+
encoder.begin_group :function
|
|
145
|
+
start = match[/^\w+\(/]
|
|
146
|
+
encoder.text_token start, :delimiter
|
|
147
|
+
if match[-1] == ?)
|
|
148
|
+
encoder.text_token match[start.size..-2], :content if match.size > start.size + 1
|
|
149
|
+
encoder.text_token ')', :delimiter
|
|
150
|
+
else
|
|
151
|
+
encoder.text_token match[start.size..-1], :content if match.size > start.size
|
|
152
|
+
end
|
|
153
|
+
encoder.end_group :function
|
|
154
|
+
|
|
155
|
+
elsif match = scan(/(?: #{RE::Dimension} | #{RE::Percentage} | #{RE::Num} )/ox)
|
|
156
|
+
encoder.text_token match, :float
|
|
157
|
+
|
|
158
|
+
elsif match = scan(/#{RE::HexColor}/o)
|
|
159
|
+
encoder.text_token match, :color
|
|
160
|
+
|
|
161
|
+
elsif match = scan(/! *important/)
|
|
162
|
+
encoder.text_token match, :important
|
|
163
|
+
|
|
164
|
+
elsif match = scan(/(?:rgb|hsl)a?\([^()\n]*\)?/)
|
|
165
|
+
encoder.text_token match, :color
|
|
166
|
+
|
|
167
|
+
elsif match = scan(RE::AtKeyword)
|
|
168
|
+
encoder.text_token match, :directive
|
|
169
|
+
|
|
170
|
+
elsif match = scan(/ [+>~:;,.=()\/] /x)
|
|
171
|
+
if match == ':'
|
|
172
|
+
value_expected = true
|
|
173
|
+
elsif match == ';'
|
|
174
|
+
value_expected = false
|
|
175
|
+
end
|
|
176
|
+
encoder.text_token match, :operator
|
|
177
|
+
|
|
178
|
+
else
|
|
179
|
+
encoder.text_token getch, :error
|
|
180
|
+
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
if options[:keep_state]
|
|
186
|
+
@state = states
|
|
187
|
+
@value_expected = value_expected
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
encoder
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
end
|
|
196
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
require 'set'
|
|
2
|
+
|
|
3
|
+
module CodeRay
|
|
4
|
+
module Scanners
|
|
5
|
+
|
|
6
|
+
# = Debug Scanner
|
|
7
|
+
#
|
|
8
|
+
# Interprets the output of the Encoders::Debug encoder (basically the inverse function).
|
|
9
|
+
class Debug < Scanner
|
|
10
|
+
|
|
11
|
+
register_for :debug
|
|
12
|
+
title 'CodeRay Token Dump Import'
|
|
13
|
+
|
|
14
|
+
protected
|
|
15
|
+
|
|
16
|
+
def setup
|
|
17
|
+
super
|
|
18
|
+
@known_token_kinds = TokenKinds.keys.map(&:to_s).to_set
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def scan_tokens encoder, options
|
|
22
|
+
|
|
23
|
+
opened_tokens = []
|
|
24
|
+
|
|
25
|
+
until eos?
|
|
26
|
+
|
|
27
|
+
if match = scan(/\s+/)
|
|
28
|
+
encoder.text_token match, :space
|
|
29
|
+
|
|
30
|
+
elsif match = scan(/ (\w+) \( ( [^\)\\]* ( \\. [^\)\\]* )* ) \)? /x)
|
|
31
|
+
if @known_token_kinds.include? self[1]
|
|
32
|
+
encoder.text_token self[2].gsub(/\\(.)/m, '\1'), self[1].to_sym
|
|
33
|
+
else
|
|
34
|
+
encoder.text_token matched, :unknown
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
elsif match = scan(/ (\w+) ([<\[]) /x)
|
|
38
|
+
if @known_token_kinds.include? self[1]
|
|
39
|
+
kind = self[1].to_sym
|
|
40
|
+
else
|
|
41
|
+
kind = :unknown
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
opened_tokens << kind
|
|
45
|
+
case self[2]
|
|
46
|
+
when '<'
|
|
47
|
+
encoder.begin_group kind
|
|
48
|
+
when '['
|
|
49
|
+
encoder.begin_line kind
|
|
50
|
+
else
|
|
51
|
+
raise 'CodeRay bug: This case should not be reached.'
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
elsif !opened_tokens.empty? && match = scan(/ > /x)
|
|
55
|
+
encoder.end_group opened_tokens.pop
|
|
56
|
+
|
|
57
|
+
elsif !opened_tokens.empty? && match = scan(/ \] /x)
|
|
58
|
+
encoder.end_line opened_tokens.pop
|
|
59
|
+
|
|
60
|
+
else
|
|
61
|
+
encoder.text_token getch, :space
|
|
62
|
+
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
encoder.end_group opened_tokens.pop until opened_tokens.empty?
|
|
68
|
+
|
|
69
|
+
encoder
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
module Scanners
|
|
3
|
+
|
|
4
|
+
# Scanner for the Delphi language (Object Pascal).
|
|
5
|
+
#
|
|
6
|
+
# Alias: +pascal+
|
|
7
|
+
class Delphi < Scanner
|
|
8
|
+
|
|
9
|
+
register_for :delphi
|
|
10
|
+
file_extension 'pas'
|
|
11
|
+
|
|
12
|
+
KEYWORDS = [
|
|
13
|
+
'and', 'array', 'as', 'at', 'asm', 'at', 'begin', 'case', 'class',
|
|
14
|
+
'const', 'constructor', 'destructor', 'dispinterface', 'div', 'do',
|
|
15
|
+
'downto', 'else', 'end', 'except', 'exports', 'file', 'finalization',
|
|
16
|
+
'finally', 'for', 'function', 'goto', 'if', 'implementation', 'in',
|
|
17
|
+
'inherited', 'initialization', 'inline', 'interface', 'is', 'label',
|
|
18
|
+
'library', 'mod', 'nil', 'not', 'object', 'of', 'or', 'out', 'packed',
|
|
19
|
+
'procedure', 'program', 'property', 'raise', 'record', 'repeat',
|
|
20
|
+
'resourcestring', 'set', 'shl', 'shr', 'string', 'then', 'threadvar',
|
|
21
|
+
'to', 'try', 'type', 'unit', 'until', 'uses', 'var', 'while', 'with',
|
|
22
|
+
'xor', 'on',
|
|
23
|
+
] # :nodoc:
|
|
24
|
+
|
|
25
|
+
DIRECTIVES = [
|
|
26
|
+
'absolute', 'abstract', 'assembler', 'at', 'automated', 'cdecl',
|
|
27
|
+
'contains', 'deprecated', 'dispid', 'dynamic', 'export',
|
|
28
|
+
'external', 'far', 'forward', 'implements', 'local',
|
|
29
|
+
'near', 'nodefault', 'on', 'overload', 'override',
|
|
30
|
+
'package', 'pascal', 'platform', 'private', 'protected', 'public',
|
|
31
|
+
'published', 'read', 'readonly', 'register', 'reintroduce',
|
|
32
|
+
'requires', 'resident', 'safecall', 'stdcall', 'stored', 'varargs',
|
|
33
|
+
'virtual', 'write', 'writeonly',
|
|
34
|
+
] # :nodoc:
|
|
35
|
+
|
|
36
|
+
IDENT_KIND = WordList::CaseIgnoring.new(:ident).
|
|
37
|
+
add(KEYWORDS, :keyword).
|
|
38
|
+
add(DIRECTIVES, :directive) # :nodoc:
|
|
39
|
+
|
|
40
|
+
NAME_FOLLOWS = WordList::CaseIgnoring.new(false).
|
|
41
|
+
add(%w(procedure function .)) # :nodoc:
|
|
42
|
+
|
|
43
|
+
protected
|
|
44
|
+
|
|
45
|
+
def scan_tokens encoder, options
|
|
46
|
+
|
|
47
|
+
state = :initial
|
|
48
|
+
last_token = ''
|
|
49
|
+
|
|
50
|
+
until eos?
|
|
51
|
+
|
|
52
|
+
if state == :initial
|
|
53
|
+
|
|
54
|
+
if match = scan(/ \s+ /x)
|
|
55
|
+
encoder.text_token match, :space
|
|
56
|
+
next
|
|
57
|
+
|
|
58
|
+
elsif match = scan(%r! \{ \$ [^}]* \}? | \(\* \$ (?: .*? \*\) | .* ) !mx)
|
|
59
|
+
encoder.text_token match, :preprocessor
|
|
60
|
+
next
|
|
61
|
+
|
|
62
|
+
elsif match = scan(%r! // [^\n]* | \{ [^}]* \}? | \(\* (?: .*? \*\) | .* ) !mx)
|
|
63
|
+
encoder.text_token match, :comment
|
|
64
|
+
next
|
|
65
|
+
|
|
66
|
+
elsif match = scan(/ <[>=]? | >=? | :=? | [-+=*\/;,@\^|\(\)\[\]] | \.\. /x)
|
|
67
|
+
encoder.text_token match, :operator
|
|
68
|
+
|
|
69
|
+
elsif match = scan(/\./)
|
|
70
|
+
encoder.text_token match, :operator
|
|
71
|
+
next if last_token == 'end'
|
|
72
|
+
|
|
73
|
+
elsif match = scan(/ [A-Za-z_][A-Za-z_0-9]* /x)
|
|
74
|
+
encoder.text_token match, NAME_FOLLOWS[last_token] ? :ident : IDENT_KIND[match]
|
|
75
|
+
|
|
76
|
+
elsif match = skip(/ ' ( [^\n']|'' ) (?:'|$) /x)
|
|
77
|
+
encoder.begin_group :char
|
|
78
|
+
encoder.text_token "'", :delimiter
|
|
79
|
+
encoder.text_token self[1], :content
|
|
80
|
+
encoder.text_token "'", :delimiter
|
|
81
|
+
encoder.end_group :char
|
|
82
|
+
next
|
|
83
|
+
|
|
84
|
+
elsif match = scan(/ ' /x)
|
|
85
|
+
encoder.begin_group :string
|
|
86
|
+
encoder.text_token match, :delimiter
|
|
87
|
+
state = :string
|
|
88
|
+
|
|
89
|
+
elsif match = scan(/ \# (?: \d+ | \$[0-9A-Fa-f]+ ) /x)
|
|
90
|
+
encoder.text_token match, :char
|
|
91
|
+
|
|
92
|
+
elsif match = scan(/ \$ [0-9A-Fa-f]+ /x)
|
|
93
|
+
encoder.text_token match, :hex
|
|
94
|
+
|
|
95
|
+
elsif match = scan(/ (?: \d+ ) (?![eE]|\.[^.]) /x)
|
|
96
|
+
encoder.text_token match, :integer
|
|
97
|
+
|
|
98
|
+
elsif match = scan(/ \d+ (?: \.\d+ (?: [eE][+-]? \d+ )? | [eE][+-]? \d+ ) /x)
|
|
99
|
+
encoder.text_token match, :float
|
|
100
|
+
|
|
101
|
+
else
|
|
102
|
+
encoder.text_token getch, :error
|
|
103
|
+
next
|
|
104
|
+
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
elsif state == :string
|
|
108
|
+
if match = scan(/[^\n']+/)
|
|
109
|
+
encoder.text_token match, :content
|
|
110
|
+
elsif match = scan(/''/)
|
|
111
|
+
encoder.text_token match, :char
|
|
112
|
+
elsif match = scan(/'/)
|
|
113
|
+
encoder.text_token match, :delimiter
|
|
114
|
+
encoder.end_group :string
|
|
115
|
+
state = :initial
|
|
116
|
+
next
|
|
117
|
+
elsif match = scan(/\n/)
|
|
118
|
+
encoder.end_group :string
|
|
119
|
+
encoder.text_token match, :space
|
|
120
|
+
state = :initial
|
|
121
|
+
else
|
|
122
|
+
raise "else case \' reached; %p not handled." % peek(1), encoder
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
else
|
|
126
|
+
raise 'else-case reached', encoder
|
|
127
|
+
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
last_token = match
|
|
131
|
+
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
if state == :string
|
|
135
|
+
encoder.end_group state
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
encoder
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
end
|
|
144
|
+
end
|