Class: DotStrings::Parser
- Inherits:
-
Object
- Object
- DotStrings::Parser
- Defined in:
- lib/dotstrings/parser.rb
Overview
Parser for .strings files.
You can use this class directly, but it is recommended to use File.parse and File.parse_file wrappers instead.
Constant Summary collapse
- TOK_SLASH =
Special tokens
'/'
- TOK_ASTERISK =
'*'
- TOK_QUOTE =
'"'
- TOK_SINGLE_QUOTE =
"'"
- TOK_BACKSLASH =
'\\'
- TOK_EQUALS =
'='
- TOK_SEMICOLON =
';'
- TOK_NEW_LINE =
"\n"
- TOK_N =
'n'
- TOK_R =
'r'
- TOK_T =
't'
- TOK_CAP_U =
'U'
- TOK_ZERO =
'0'
- TOK_HEX_DIGIT =
/[0-9a-fA-F]/.freeze
- STATE_START =
States
0
- STATE_COMMENT_START =
1
- STATE_COMMENT =
2
- STATE_MULTILINE_COMMENT =
3
- STATE_COMMENT_END =
4
- STATE_KEY =
5
- STATE_KEY_END =
6
- STATE_VALUE_SEPARATOR =
7
- STATE_VALUE =
8
- STATE_VALUE_END =
9
- STATE_UNICODE =
10
- STATE_UNICODE_SURROGATE =
11
- STATE_UNICODE_SURROGATE_U =
12
Instance Method Summary collapse
-
#<<(data) ⇒ Object
Feeds data to the parser.
-
#initialize(strict: true) ⇒ Parser
constructor
Returns a new Parser instance.
-
#on_item(&block) ⇒ Object
Specifies a block to be called when a new item is parsed.
Constructor Details
#initialize(strict: true) ⇒ Parser
Returns a new Parser instance.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/dotstrings/parser.rb', line 49 def initialize(strict: true) @strict = strict @state = STATE_START @temp_state = nil @buffer = [] @unicode_buffer = [] @high_surrogate = nil @escaping = false @current_comment = nil @current_key = nil @current_value = nil @item_block = nil @offset = 0 @line = 1 @column = 1 end |
Instance Method Details
#<<(data) ⇒ Object
Feeds data to the parser.
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 |
# File 'lib/dotstrings/parser.rb', line 82 def <<(data) data.each_char do |ch| case @state when STATE_START start_value(ch) when STATE_COMMENT_START case ch when TOK_SLASH @state = STATE_COMMENT when TOK_ASTERISK @state = STATE_MULTILINE_COMMENT else raise_error("Unexpected character '#{ch}'") end when STATE_COMMENT if ch == TOK_NEW_LINE @state = STATE_COMMENT_END @current_comment = @buffer.join.strip @buffer.clear else @buffer << ch end when STATE_MULTILINE_COMMENT if ch == TOK_SLASH && @buffer.last == TOK_ASTERISK @state = STATE_COMMENT_END @current_comment = @buffer.slice(0, @buffer.length - 1).join.strip @buffer.clear else @buffer << ch end when STATE_COMMENT_END comment_end(ch) when STATE_KEY parse_string(ch) do |key| @current_key = key @state = STATE_KEY_END end when STATE_KEY_END if ch == TOK_EQUALS @state = STATE_VALUE_SEPARATOR else raise_error("Unexpected character '#{ch}', expecting '#{TOK_EQUALS}'") unless whitespace?(ch) end when STATE_VALUE_SEPARATOR if ch == TOK_QUOTE @state = STATE_VALUE else raise_error("Unexpected character '#{ch}'") unless whitespace?(ch) end when STATE_VALUE parse_string(ch) do |value| @current_value = value @state = STATE_VALUE_END @item_block&.call(Item.new( comment: @current_comment, key: @current_key, value: @current_value )) end when STATE_VALUE_END if ch == TOK_SEMICOLON @state = STATE_START else raise_error("Unexpected character '#{ch}', expecting '#{TOK_SEMICOLON}'") unless whitespace?(ch) end when STATE_UNICODE parse_unicode(ch) do |unicode_ch| @buffer << unicode_ch # Restore state @state = @temp_state end when STATE_UNICODE_SURROGATE if ch == TOK_BACKSLASH @state = STATE_UNICODE_SURROGATE_U else raise_error("Unexpected character '#{ch}', expecting another unicode codepoint") end when STATE_UNICODE_SURROGATE_U if ch == TOK_CAP_U @state = STATE_UNICODE else raise_error("Unexpected character '#{ch}', expecting '#{TOK_CAP_U}'") end end update_position(ch) end end |
#on_item(&block) ⇒ Object
Specifies a block to be called when a new item is parsed.
74 75 76 |
# File 'lib/dotstrings/parser.rb', line 74 def on_item(&block) @item_block = block end |