Module | RubyParserStuff |
In: |
lib/ruby_parser_extras.rb
|
VERSION | = | '3.0.1' unless constants.include? "VERSION" |
canonicalize_conditions | [RW] |
Canonicalize conditionals. Eg:
not x ? a : b becomes: x ? b : a |
comments | [R] | |
env | [R] | |
file | [RW] | |
in_def | [RW] | |
in_single | [RW] | |
lexer | [RW] |
Returns a UTF-8 encoded string after processing BOMs and magic encoding comments.
Holy crap… ok. Here goes:
Ruby‘s file handling and encoding support is insane. We need to be able to lex a file. The lexer file is explicitly UTF-8 to make things cleaner. This allows us to deal with extended chars in class and method names. In order to do this, we need to encode all input source files as UTF-8. First, we look for a UTF-8 BOM by looking at the first line while forcing its encoding to ASCII-8BIT. If we find a BOM, we strip it and set the expected encoding to UTF-8. Then, we search for a magic encoding comment. If found, it overrides the BOM. Finally, we force the encoding of the input string to whatever was found, and then encode that to UTF-8 for compatibility with the lexer.