Class: Infoboxer::Parser::Context
- Inherits:
-
Object
- Object
- Infoboxer::Parser::Context
- Defined in:
- lib/infoboxer/parser/context.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#inline_eol_sign ⇒ Object
readonly
Returns the value of attribute inline_eol_sign.
-
#lineno ⇒ Object
readonly
Returns the value of attribute lineno.
-
#next_lines ⇒ Object
readonly
Returns the value of attribute next_lines.
-
#traits ⇒ Object
readonly
Returns the value of attribute traits.
Instance Method Summary collapse
- #check(re) ⇒ Object
- #colno ⇒ Object
-
#eat_matched?(str) ⇒ Boolean
check which works only once.
- #eof? ⇒ Boolean
- #eol? ⇒ Boolean
-
#fail!(text) ⇒ Object
basic services.
-
#initialize(text, traits = nil) ⇒ Context
constructor
A new instance of Context.
- #inline_eol?(exclude = nil) ⇒ Boolean
- #inspect ⇒ Object
- #matched ⇒ Object
- #matched?(re) ⇒ Boolean
-
#matched_inline?(re) ⇒ Boolean
state inspection.
-
#next! ⇒ Object
lines navigation.
- #pop_eol_sign ⇒ Object
- #prev! ⇒ Object
- #push_eol_sign(re) ⇒ Object
- #rest ⇒ Object (also: #current)
-
#scan(re) ⇒ Object
scanning.
- #scan_continued_until(re, leave_pattern = false) ⇒ Object
- #scan_until(re, leave_pattern = false) ⇒ Object
- #skip(re) ⇒ Object
- #unscan_matched! ⇒ Object
Constructor Details
#initialize(text, traits = nil) ⇒ Context
Returns a new instance of Context.
11 12 13 14 15 16 17 18 19 |
# File 'lib/infoboxer/parser/context.rb', line 11 def initialize(text, traits = nil) @lines = text .gsub(/<!--.*?-->/m, '') # FIXME: will also kill comments inside <nowiki> tag .split(/[\r\n]/) @lineno = -1 @traits = traits || MediaWiki::Traits.default @scanner = StringScanner.new('') next! end |
Instance Attribute Details
#inline_eol_sign ⇒ Object (readonly)
Returns the value of attribute inline_eol_sign.
101 102 103 |
# File 'lib/infoboxer/parser/context.rb', line 101 def inline_eol_sign @inline_eol_sign end |
#lineno ⇒ Object (readonly)
Returns the value of attribute lineno.
8 9 10 |
# File 'lib/infoboxer/parser/context.rb', line 8 def lineno @lineno end |
#next_lines ⇒ Object (readonly)
Returns the value of attribute next_lines.
21 22 23 |
# File 'lib/infoboxer/parser/context.rb', line 21 def next_lines @next_lines end |
#traits ⇒ Object (readonly)
Returns the value of attribute traits.
9 10 11 |
# File 'lib/infoboxer/parser/context.rb', line 9 def traits @traits end |
Instance Method Details
#check(re) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/infoboxer/parser/context.rb', line 71 def check(re) res = @scanner.check(re) @matched = nil @rest = nil res end |
#colno ⇒ Object
23 24 25 |
# File 'lib/infoboxer/parser/context.rb', line 23 def colno @scanner&.pos || 0 end |
#eat_matched?(str) ⇒ Boolean
check which works only once
32 33 34 35 36 37 |
# File 'lib/infoboxer/parser/context.rb', line 32 def eat_matched?(str) return false unless matched == str @matched = 'DUMMY' true end |
#eof? ⇒ Boolean
54 55 56 57 |
# File 'lib/infoboxer/parser/context.rb', line 54 def eof? !next_lines || # we are after the file end next_lines.empty? && eol? end |
#eol? ⇒ Boolean
147 148 149 |
# File 'lib/infoboxer/parser/context.rb', line 147 def eol? !current || current.empty? end |
#fail!(text) ⇒ Object
basic services
152 153 154 |
# File 'lib/infoboxer/parser/context.rb', line 152 def fail!(text) fail(ParsingError, "#{text} at line #{@lineno}:\n\t#{current}") end |
#inline_eol?(exclude = nil) ⇒ Boolean
103 104 105 106 107 108 109 110 |
# File 'lib/infoboxer/parser/context.rb', line 103 def inline_eol?(exclude = nil) # not using StringScanner#check, as it will change #matched value eol? || ( (current =~ %r[^(</ref>|}})] || @inline_eol_sign && current =~ @inline_eol_sign) && (!exclude || Regexp.last_match(1) !~ exclude) ) # FIXME: ugly, but no idea of prettier solution end |
#inspect ⇒ Object
59 60 61 |
# File 'lib/infoboxer/parser/context.rb', line 59 def inspect "#<Context(line #{lineno} of #{@lines.count}: #{current})>" end |
#matched ⇒ Object
27 28 29 |
# File 'lib/infoboxer/parser/context.rb', line 27 def matched @matched ||= @scanner&.matched end |
#matched?(re) ⇒ Boolean
143 144 145 |
# File 'lib/infoboxer/parser/context.rb', line 143 def matched?(re) re && matched =~ re end |
#matched_inline?(re) ⇒ Boolean
state inspection
133 134 135 136 137 138 139 140 141 |
# File 'lib/infoboxer/parser/context.rb', line 133 def matched_inline?(re) if re.nil? matched.empty? && eol? elsif re.inspect.start_with?('/^') # was it REALLY at the beginning of the line?.. @scanner.pos == matched.length && matched =~ re else matched =~ re end end |
#next! ⇒ Object
lines navigation
46 47 48 |
# File 'lib/infoboxer/parser/context.rb', line 46 def next! shift(+1) end |
#pop_eol_sign ⇒ Object
97 98 99 |
# File 'lib/infoboxer/parser/context.rb', line 97 def pop_eol_sign @inline_eol_sign = nil end |
#prev! ⇒ Object
50 51 52 |
# File 'lib/infoboxer/parser/context.rb', line 50 def prev! shift(-1) end |
#push_eol_sign(re) ⇒ Object
93 94 95 |
# File 'lib/infoboxer/parser/context.rb', line 93 def push_eol_sign(re) @inline_eol_sign = re end |
#rest ⇒ Object Also known as: current
39 40 41 |
# File 'lib/infoboxer/parser/context.rb', line 39 def rest @rest ||= @scanner&.rest end |
#scan(re) ⇒ Object
scanning
64 65 66 67 68 69 |
# File 'lib/infoboxer/parser/context.rb', line 64 def scan(re) res = @scanner.scan(re) @matched = nil @rest = nil res end |
#scan_continued_until(re, leave_pattern = false) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/infoboxer/parser/context.rb', line 112 def scan_continued_until(re, leave_pattern = false) res = +'' loop do chunk = _scan_until(re) case matched when re res << chunk break when nil res << rest << "\n" next! eof? && fail!("Unfinished scan: #{re} not found") end end res[/#{re}\Z/] = '' unless leave_pattern res end |
#scan_until(re, leave_pattern = false) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/infoboxer/parser/context.rb', line 85 def scan_until(re, leave_pattern = false) guard_eof! res = _scan_until(re) res[matched] = '' if res && !leave_pattern res end |
#skip(re) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/infoboxer/parser/context.rb', line 78 def skip(re) res = @scanner.skip(re) @matched = nil @rest = nil res end |
#unscan_matched! ⇒ Object
156 157 158 159 160 161 |
# File 'lib/infoboxer/parser/context.rb', line 156 def unscan_matched! return unless @matched @scanner.pos -= @matched.size @rest = nil end |