Class: Bade::Parser
- Inherits:
-
Object
show all
- Defined in:
- lib/bade/parser.rb,
lib/bade/parser/parser_tag.rb,
lib/bade/parser/parser_text.rb,
lib/bade/parser/parser_lines.rb,
lib/bade/parser/parser_mixin.rb,
lib/bade/parser/parser_constants.rb,
lib/bade/parser/parser_ruby_code.rb
Overview
Class to parse input string into AST::Document
Defined Under Namespace
Modules: LineIndicatorRegexps, MixinRegexps, ParseRubyCodeRegexps, TagRegexps, TextRegexps
Classes: ParserInternalError, SyntaxError
Constant Summary
collapse
- WORD_RE =
''.respond_to?(:encoding) ? '\p{Word}' : '\w'
- NAME_RE_STRING =
"(#{WORD_RE}(?:#{WORD_RE}|:|-|_)*)".freeze
- ATTR_NAME_RE_STRING =
"\\A\\s*#{NAME_RE_STRING}".freeze
- CODE_ATTR_RE =
/#{ATTR_NAME_RE_STRING}\s*&?:\s*/
- TAG_RE =
/\A#{NAME_RE_STRING}/
- CLASS_TAG_RE =
/\A\.#{NAME_RE_STRING}/
- ID_TAG_RE =
/\A##{NAME_RE_STRING}/
- RUBY_DELIMITERS_REVERSE =
{
'(' => ')',
'[' => ']',
'{' => '}',
}.freeze
- RUBY_QUOTES =
%w(' ").freeze
- RUBY_NOT_NESTABLE_DELIMITERS =
RUBY_QUOTES
- RUBY_START_DELIMITERS =
(%w(\( [ {) + RUBY_NOT_NESTABLE_DELIMITERS).freeze
- RUBY_END_DELIMITERS =
(%w(\) ] }) + RUBY_NOT_NESTABLE_DELIMITERS).freeze
- RUBY_ALL_DELIMITERS =
(RUBY_START_DELIMITERS + RUBY_END_DELIMITERS).uniq.freeze
- RUBY_START_DELIMITERS_RE =
/\A[#{Regexp.escape RUBY_START_DELIMITERS.join}]/
- RUBY_END_DELIMITERS_RE =
/\A[#{Regexp.escape RUBY_END_DELIMITERS.join}]/
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(tabsize: 4, file_path: nil) ⇒ Parser
Returns a new instance of Parser.
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/bade/parser.rb', line 56
def initialize(tabsize: 4, file_path: nil)
@line = ''
@tabsize = tabsize
@file_path = file_path
@tab_re = /\G((?: {#{tabsize}})*) {0,#{tabsize - 1}}\t/
@tab = '\1' + ' ' * tabsize
reset
end
|
Instance Attribute Details
47
48
49
|
# File 'lib/bade/parser.rb', line 47
def dependency_paths
@dependency_paths
end
|
#file_path ⇒ String
51
52
53
|
# File 'lib/bade/parser.rb', line 51
def file_path
@file_path
end
|
Instance Method Details
#append_node(type, indent: @indents.length, add: false, value: nil) ⇒ Object
Append element to stacks and result tree
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/bade/parser.rb', line 104
def append_node(type, indent: @indents.length, add: false, value: nil)
@stacks << @stacks.last.dup while indent >= @stacks.length
parent = @stacks[indent].last
node = AST::NodeRegistrator.create(type, @lineno)
parent.children << node
node.value = value unless value.nil?
@stacks[indent] << node if add
node
end
|
#fixed_trailing_colon(value) ⇒ Object
137
138
139
140
141
142
143
144
|
# File 'lib/bade/parser.rb', line 137
def fixed_trailing_colon(value)
if value.is_a?(String) && value.end_with?(':')
value = value.remove_last
@line.prepend(':')
end
value
end
|
#get_indent(line) ⇒ Int
Calculate indent for line
96
97
98
|
# File 'lib/bade/parser.rb', line 96
def get_indent(line)
line.get_indent(@tabsize)
end
|
#next_line ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/bade/parser/parser_lines.rb', line 53
def next_line
if @lines.empty?
@orig_line = @line = nil
last_newlines = remove_last_newlines
@root.children += last_newlines
nil
else
@orig_line = @lines.shift
@lineno += 1
@line = @orig_line.dup
end
end
|
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/bade/parser.rb', line 71
def parse(str)
@document = AST::Document.new(file_path: file_path)
@root = @document.root
@dependency_paths = []
if str.is_a?(Array)
reset(str, [[@root]])
else
reset(str.split(/\r?\n/, -1), [[@root]]) end
parse_line while next_line
reset
@document
end
|
#parse_import ⇒ Object
127
128
129
130
131
132
133
|
# File 'lib/bade/parser.rb', line 127
def parse_import
path = eval(@line) append_node(:import, value: path)
@dependency_paths << path unless @dependency_paths.include?(path)
end
|
#parse_line ⇒ Object
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
|
# File 'lib/bade/parser/parser_lines.rb', line 68
def parse_line
if @line.strip.empty?
append_node(:newline) unless @lines.empty?
return
end
indent = get_indent(@line)
@line.remove_indent!(indent, @tabsize)
expecting_indentation = @stacks.length > @indents.length
if indent > @indents.last
@indents << indent
else
if expecting_indentation
last_newlines = remove_last_newlines
@stacks.pop
new_node = @stacks.last.last
new_node.children += last_newlines
end
while indent < @indents.last
last_newlines = remove_last_newlines
@indents.pop
@stacks.pop
new_node = @stacks.last.last
new_node.children += last_newlines
end
while !@stacks[indent].nil? && indent < @stacks[indent].length - 1
last_newlines = remove_last_newlines
@stacks[indent].pop
new_node = @stacks.last.last
new_node.children += last_newlines
end
syntax_error('Malformed indentation') if indent != @indents.last
end
parse_line_indicators
end
|
#parse_line_indicators(add_newline: true) ⇒ Object
#parse_mixin_call(mixin_name) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/bade/parser/parser_mixin.rb', line 23
def parse_mixin_call(mixin_name)
mixin_name = fixed_trailing_colon(mixin_name)
mixin_node = append_node(:mixin_call, add: true)
mixin_node.name = mixin_name
parse_mixin_call_params
case @line
when MixinRegexps::TEXT_START
@line = $'
parse_text
when MixinRegexps::BLOCK_EXPANSION
@line = $'
parse_line_indicators(add_newline: false)
when MixinRegexps::OUTPUT_CODE
parse_line_indicators(add_newline: false)
when ''
else
syntax_error "Unknown symbol after mixin calling, line = `#{@line}'"
end
end
|
#parse_mixin_call_params ⇒ Object
#parse_mixin_declaration(mixin_name) ⇒ Object
91
92
93
94
95
96
|
# File 'lib/bade/parser/parser_mixin.rb', line 91
def parse_mixin_declaration(mixin_name)
mixin_node = append_node(:mixin_decl, add: true)
mixin_node.name = mixin_name
parse_mixin_declaration_params
end
|
#parse_mixin_declaration_params ⇒ Object
#parse_ruby_code(outer_delimiters) ⇒ Void
Parse ruby code, ended with outer delimiters
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/bade/parser/parser_ruby_code.rb', line 19
def parse_ruby_code(outer_delimiters)
code = String.new
end_re = if outer_delimiters.is_a?(Regexp)
outer_delimiters
else
/\A\s*[#{Regexp.escape outer_delimiters.to_s}]/
end
delimiters = []
until @line.empty? || (delimiters.count == 0 && @line =~ end_re)
char = @line[0]
if char == '\\' && RUBY_ALL_DELIMITERS.include?(@line[1])
code << @line.slice!(0, 2)
next
end
case char
when RUBY_START_DELIMITERS_RE
if RUBY_NOT_NESTABLE_DELIMITERS.include?(char) && delimiters.last == char
delimiters.pop
else
delimiters << char
end
when RUBY_END_DELIMITERS_RE
delimiters.pop if char == RUBY_DELIMITERS_REVERSE[delimiters.last]
end
code << @line.slice!(0)
end
syntax_error('Unexpected end of ruby code') unless delimiters.empty?
code.strip
end
|
#parse_tag(tag) ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/bade/parser/parser_tag.rb', line 19
def parse_tag(tag)
tag = fixed_trailing_colon(tag)
if tag.is_a?(AST::Node)
tag_node = tag
else
tag_node = append_node(:tag, add: true)
tag_node.name = tag
end
parse_tag_attributes
case @line
when TagRegexps::BLOCK_EXPANSION
@line = $'
parse_line_indicators(add_newline: false)
when TagRegexps::OUTPUT_CODE
parse_line_indicators(add_newline: false)
when CLASS_TAG_RE
@line = $'
attr_node = append_node(:tag_attr)
attr_node.name = 'class'
attr_node.value = fixed_trailing_colon($1).single_quote
parse_tag tag_node
when ID_TAG_RE
@line = $'
attr_node = append_node(:tag_attr)
attr_node.name = 'id'
attr_node.value = fixed_trailing_colon($1).single_quote
parse_tag tag_node
when TagRegexps::TEXT_START
@line = $'
parse_text
when ''
else
syntax_error "Unknown symbol after tag definition #{@line}"
end
end
|
#parse_tag_attributes ⇒ Object
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
|
# File 'lib/bade/parser/parser_tag.rb', line 74
def parse_tag_attributes
return unless @line.start_with?('(')
@line.remove_first!
loop do
case @line
when CODE_ATTR_RE
@line = $'
attr_node = append_node(:tag_attr)
attr_node.name = $1
attr_node.value = parse_ruby_code(ParseRubyCodeRegexps::END_PARAMS_ARG)
when TagRegexps::PARAMS_ARGS_DELIMITER
@line = $'
next
when TagRegexps::PARAMS_END
@line = $'
break
else
@line.lstrip!
syntax_error('Expected attribute') unless @line.empty?
append_node(:newline)
syntax_error('Expected closing tag attributes delimiter `)`') if @lines.empty?
next_line
end
end
end
|
#parse_text ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/bade/parser/parser_text.rb', line 13
def parse_text
new_index = @line.index(TextRegexps::INTERPOLATION_START)
if new_index.nil?
append_node(:static_text, value: @line)
return
end
unparsed_part = String.new
while (new_index = @line.index(TextRegexps::INTERPOLATION_START))
if $1.nil?
static_part = unparsed_part + @line.remove_first!(new_index)
append_node(:static_text, value: static_part)
@line.remove_first!(2)
dynamic_part = parse_ruby_code(TextRegexps::INTERPOLATION_END)
node = append_node(:output, value: dynamic_part)
node.escaped = $2 == '&'
@line.remove_first!
unparsed_part = String.new
else
unparsed_part << @line.remove_first!(new_index)
@line.remove_first! unparsed_part << @line.remove_first!(2) end
end
append_node(:static_text, value: unparsed_part + @line) unless @line.empty?
end
|
#parse_text_block(first_line, text_indent = nil) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/bade/parser/parser_text.rb', line 49
def parse_text_block(first_line, text_indent = nil)
if !first_line || first_line.empty?
text_indent = nil
else
@line = first_line
parse_text
end
until @lines.empty?
if @lines.first.blank?
next_line
append_node(:newline)
else
indent = get_indent(@lines.first)
break if indent <= @indents.last
next_line
@line.remove_indent!(text_indent ? text_indent : indent, @tabsize)
parse_text
text_indent ||= indent
end
end
end
|
121
122
123
124
125
|
# File 'lib/bade/parser.rb', line 121
def remove_last_newlines
last_node = @stacks.last.last
last_newlines_count = last_node.children.rcount_matching { |n| n.type == :newline }
last_node.children.pop(last_newlines_count)
end
|
#reset(lines = nil, stacks = nil) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/bade/parser/parser_lines.rb', line 24
def reset(lines = nil, stacks = nil)
@indents = [0]
@stacks = stacks
@lineno = 0
@lines = lines
@line = @orig_line = nil
end
|
#syntax_error(message) ⇒ Object
152
153
154
155
|
# File 'lib/bade/parser.rb', line 152
def syntax_error(message)
column = @orig_line && @line ? @orig_line.size - @line.size : 0
raise SyntaxError.new(message, file_path, @orig_line, @lineno, column)
end
|