Skip to content

Crystal::SyntaxHighlighter cannot handle regex escape sequences #16390

@Blacksmoke16

Description

@Blacksmoke16
require "crystal/syntax_highlighter/colorize"

code = <<-'CR'
"Name: Alice".match!(/Name: (\w+)/)
CR

pp Crystal::SyntaxHighlighter::Colorize.highlight(code)
$ crystal test.cr
Unhandled exception: unknown token: 'w' (Crystal::SyntaxException)
  from /usr/lib/crystal/compiler/crystal/syntax/lexer.cr:2950:7 in 'raise'
  from /usr/lib/crystal/compiler/crystal/syntax/lexer.cr:2949:5 in 'raise'
  from /usr/lib/crystal/compiler/crystal/syntax/lexer.cr:2942:7 in 'unknown_token'
  from /usr/lib/crystal/compiler/crystal/syntax/lexer.cr:158:14 in 'next_token'
  from /usr/lib/crystal/crystal/syntax_highlighter.cr:89:7 in 'highlight_normal_state'
  from /usr/lib/crystal/crystal/syntax_highlighter.cr:80:11 in 'highlight_normal_state'
  from /usr/lib/crystal/crystal/syntax_highlighter.cr:14:5 in 'highlight'
  from /usr/lib/crystal/crystal/syntax_highlighter/colorize.cr:19:5 in 'highlight'
  from /usr/lib/crystal/crystal/syntax_highlighter/colorize.cr:25:7 in 'highlight'
  from test.cr:7:4 in '__crystal_main'
  from /usr/lib/crystal/crystal/main.cr:129:5 in 'main_user_code'
  from /usr/lib/crystal/crystal/main.cr:115:7 in 'main'
  from /usr/lib/crystal/crystal/system/unix/main.cr:9:3 in 'main'
  from /usr/lib/libc.so.6 in '??'
  from /usr/lib/libc.so.6 in '__libc_start_main'
  from /home/george/.cache/crystal/crystal-run-test.tmp in '_start'
  from ???
$ crystal --version
Crystal 1.18.2 (2025-10-21)

LLVM: 21.1.5
Default target: x86_64-pc-linux-gnu

Also reproduces on master.

Originally posted by @Blacksmoke16 in #16378 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind:bugA bug in the code. Does not apply to documentation, specs, etc.topic:compiler:parser

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions