2010-04-01 03:45:16 -04:00
|
|
|
require 'strscan'
|
|
|
|
|
|
|
|
##
|
|
|
|
# A recursive-descent parser for RDoc markup.
|
|
|
|
#
|
|
|
|
# The parser tokenizes an input string then parses the tokens into a Document.
|
|
|
|
# Documents can be converted into output formats by writing a visitor like
|
|
|
|
# RDoc::Markup::ToHTML.
|
|
|
|
#
|
|
|
|
# The parser only handles the block-level constructs Paragraph, List,
|
|
|
|
# ListItem, Heading, Verbatim, BlankLine and Rule. Inline markup such as
|
|
|
|
# <tt>\+blah\+</tt> is handled separately by RDoc::Markup::AttributeManager.
|
|
|
|
#
|
|
|
|
# To see what markup the Parser implements read RDoc. To see how to use
|
|
|
|
# RDoc markup to format text in your program read RDoc::Markup.
|
|
|
|
|
|
|
|
class RDoc::Markup::Parser
|
|
|
|
|
|
|
|
include RDoc::Text
|
|
|
|
|
|
|
|
##
|
|
|
|
# List token types
|
|
|
|
|
|
|
|
LIST_TOKENS = [
|
|
|
|
:BULLET,
|
|
|
|
:LABEL,
|
|
|
|
:LALPHA,
|
|
|
|
:NOTE,
|
|
|
|
:NUMBER,
|
|
|
|
:UALPHA,
|
|
|
|
]
|
|
|
|
|
|
|
|
##
|
|
|
|
# Parser error subclass
|
|
|
|
|
|
|
|
class Error < RuntimeError; end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Raised when the parser is unable to handle the given markup
|
|
|
|
|
|
|
|
class ParseError < Error; end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Enables display of debugging information
|
|
|
|
|
|
|
|
attr_accessor :debug
|
|
|
|
|
|
|
|
##
|
|
|
|
# Token accessor
|
|
|
|
|
|
|
|
attr_reader :tokens
|
|
|
|
|
|
|
|
##
|
2012-11-26 23:28:14 -05:00
|
|
|
# Parses +str+ into a Document.
|
|
|
|
#
|
|
|
|
# Use RDoc::Markup#parse instead of this method.
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
def self.parse str
|
|
|
|
parser = new
|
|
|
|
parser.tokenize str
|
2010-12-19 22:22:49 -05:00
|
|
|
doc = RDoc::Markup::Document.new
|
|
|
|
parser.parse doc
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Returns a token stream for +str+, for testing
|
|
|
|
|
|
|
|
def self.tokenize str
|
|
|
|
parser = new
|
|
|
|
parser.tokenize str
|
|
|
|
parser.tokens
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new Parser. See also ::parse
|
|
|
|
|
|
|
|
def initialize
|
2012-11-26 23:28:14 -05:00
|
|
|
@binary_input = nil
|
|
|
|
@current_token = nil
|
|
|
|
@debug = false
|
|
|
|
@have_encoding = Object.const_defined? :Encoding
|
2012-11-29 18:34:29 -05:00
|
|
|
@have_byteslice = ''.respond_to? :byteslice
|
|
|
|
@input = nil
|
2012-11-26 23:28:14 -05:00
|
|
|
@input_encoding = nil
|
|
|
|
@line = 0
|
|
|
|
@line_pos = 0
|
|
|
|
@s = nil
|
|
|
|
@tokens = []
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Builds a Heading of +level+
|
|
|
|
|
|
|
|
def build_heading level
|
2011-08-01 01:25:13 -04:00
|
|
|
type, text, = get
|
|
|
|
|
|
|
|
text = case type
|
|
|
|
when :TEXT then
|
|
|
|
skip :NEWLINE
|
|
|
|
text
|
|
|
|
else
|
|
|
|
unget
|
|
|
|
''
|
|
|
|
end
|
|
|
|
|
|
|
|
RDoc::Markup::Heading.new level, text
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Builds a List flush to +margin+
|
|
|
|
|
|
|
|
def build_list margin
|
|
|
|
p :list_start => margin if @debug
|
|
|
|
|
|
|
|
list = RDoc::Markup::List.new
|
2012-11-26 23:28:14 -05:00
|
|
|
label = nil
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
until @tokens.empty? do
|
|
|
|
type, data, column, = get
|
|
|
|
|
|
|
|
case type
|
2012-11-26 23:28:14 -05:00
|
|
|
when *LIST_TOKENS then
|
2010-12-19 22:22:49 -05:00
|
|
|
if column < margin || (list.type && list.type != type) then
|
2010-04-01 03:45:16 -04:00
|
|
|
unget
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
list.type = type
|
|
|
|
peek_type, _, column, = peek_token
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
case type
|
|
|
|
when :NOTE, :LABEL then
|
2012-11-26 23:28:14 -05:00
|
|
|
label = [] unless label
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
if peek_type == :NEWLINE then
|
|
|
|
# description not on the same line as LABEL/NOTE
|
|
|
|
# skip the trailing newline & any blank lines below
|
|
|
|
while peek_type == :NEWLINE
|
|
|
|
get
|
|
|
|
peek_type, _, column, = peek_token
|
|
|
|
end
|
|
|
|
|
|
|
|
# we may be:
|
|
|
|
# - at end of stream
|
|
|
|
# - at a column < margin:
|
|
|
|
# [text]
|
|
|
|
# blah blah blah
|
|
|
|
# - at the same column, but with a different type of list item
|
|
|
|
# [text]
|
|
|
|
# * blah blah
|
|
|
|
# - at the same column, with the same type of list item
|
|
|
|
# [one]
|
|
|
|
# [two]
|
|
|
|
# In all cases, we have an empty description.
|
|
|
|
# In the last case only, we continue.
|
|
|
|
if peek_type.nil? || column < margin then
|
2012-11-26 23:28:14 -05:00
|
|
|
empty = true
|
2010-12-19 22:22:49 -05:00
|
|
|
elsif column == margin then
|
|
|
|
case peek_type
|
|
|
|
when type
|
2012-11-26 23:28:14 -05:00
|
|
|
empty = :continue
|
2010-12-19 22:22:49 -05:00
|
|
|
when *LIST_TOKENS
|
2012-11-26 23:28:14 -05:00
|
|
|
empty = true
|
2010-12-19 22:22:49 -05:00
|
|
|
else
|
2012-11-26 23:28:14 -05:00
|
|
|
empty = false
|
2010-12-19 22:22:49 -05:00
|
|
|
end
|
|
|
|
else
|
2012-11-26 23:28:14 -05:00
|
|
|
empty = false
|
2010-12-19 22:22:49 -05:00
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
if empty then
|
|
|
|
label << data
|
|
|
|
next if empty == :continue
|
|
|
|
break
|
2010-12-19 22:22:49 -05:00
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
data = nil
|
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
if label then
|
|
|
|
data = label << data
|
|
|
|
label = nil
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
list_item = RDoc::Markup::ListItem.new data
|
|
|
|
parse list_item, column
|
|
|
|
list << list_item
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
else
|
|
|
|
unget
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
p :list_end => margin if @debug
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
if list.empty? then
|
|
|
|
return nil unless label
|
|
|
|
return nil unless [:LABEL, :NOTE].include? list.type
|
|
|
|
|
|
|
|
list_item = RDoc::Markup::ListItem.new label, RDoc::Markup::BlankLine.new
|
|
|
|
list << list_item
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
list
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Builds a Paragraph that is flush to +margin+
|
|
|
|
|
|
|
|
def build_paragraph margin
|
|
|
|
p :paragraph_start => margin if @debug
|
|
|
|
|
|
|
|
paragraph = RDoc::Markup::Paragraph.new
|
|
|
|
|
|
|
|
until @tokens.empty? do
|
|
|
|
type, data, column, = get
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
if type == :TEXT and column == margin then
|
2010-04-01 03:45:16 -04:00
|
|
|
paragraph << data
|
2012-11-26 23:28:14 -05:00
|
|
|
|
|
|
|
break if peek_token.first == :BREAK
|
|
|
|
|
|
|
|
data << ' ' if skip :NEWLINE
|
2010-04-01 03:45:16 -04:00
|
|
|
else
|
|
|
|
unget
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
paragraph.parts.last.sub!(/ \z/, '') # cleanup
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
p :paragraph_end => margin if @debug
|
|
|
|
|
|
|
|
paragraph
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
# Builds a Verbatim that is indented from +margin+.
|
|
|
|
#
|
|
|
|
# The verbatim block is shifted left (the least indented lines start in
|
|
|
|
# column 0). Each part of the verbatim is one line of text, always
|
|
|
|
# terminated by a newline. Blank lines always consist of a single newline
|
|
|
|
# character, and there is never a single newline at the end of the verbatim.
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
def build_verbatim margin
|
|
|
|
p :verbatim_begin => margin if @debug
|
|
|
|
verbatim = RDoc::Markup::Verbatim.new
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
min_indent = nil
|
|
|
|
generate_leading_spaces = true
|
|
|
|
line = ''
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
until @tokens.empty? do
|
|
|
|
type, data, column, = get
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
if type == :NEWLINE then
|
|
|
|
line << data
|
|
|
|
verbatim << line
|
|
|
|
line = ''
|
|
|
|
generate_leading_spaces = true
|
|
|
|
next
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
if column <= margin
|
|
|
|
unget
|
|
|
|
break
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
if generate_leading_spaces then
|
|
|
|
indent = column - margin
|
|
|
|
line << ' ' * indent
|
|
|
|
min_indent = indent if min_indent.nil? || indent < min_indent
|
|
|
|
generate_leading_spaces = false
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
case type
|
|
|
|
when :HEADER then
|
|
|
|
line << '=' * data
|
2010-04-01 03:45:16 -04:00
|
|
|
_, _, peek_column, = peek_token
|
|
|
|
peek_column ||= column + data
|
2010-12-19 22:22:49 -05:00
|
|
|
indent = peek_column - column - data
|
|
|
|
line << ' ' * indent
|
2010-04-01 03:45:16 -04:00
|
|
|
when :RULE then
|
|
|
|
width = 2 + data
|
2010-12-19 22:22:49 -05:00
|
|
|
line << '-' * width
|
2010-04-01 03:45:16 -04:00
|
|
|
_, _, peek_column, = peek_token
|
2010-12-19 22:22:49 -05:00
|
|
|
peek_column ||= column + width
|
|
|
|
indent = peek_column - column - width
|
|
|
|
line << ' ' * indent
|
2012-11-26 23:28:14 -05:00
|
|
|
when :BREAK, :TEXT then
|
2010-12-19 22:22:49 -05:00
|
|
|
line << data
|
|
|
|
else # *LIST_TOKENS
|
2010-04-01 03:45:16 -04:00
|
|
|
list_marker = case type
|
2010-12-19 22:22:49 -05:00
|
|
|
when :BULLET then data
|
|
|
|
when :LABEL then "[#{data}]"
|
|
|
|
when :NOTE then "#{data}::"
|
|
|
|
else # :LALPHA, :NUMBER, :UALPHA
|
|
|
|
"#{data}."
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
2010-12-19 22:22:49 -05:00
|
|
|
line << list_marker
|
|
|
|
peek_type, _, peek_column = peek_token
|
|
|
|
unless peek_type == :NEWLINE then
|
|
|
|
peek_column ||= column + list_marker.length
|
|
|
|
indent = peek_column - column - list_marker.length
|
|
|
|
line << ' ' * indent
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
verbatim << line << "\n" unless line.empty?
|
|
|
|
verbatim.parts.each { |p| p.slice!(0, min_indent) unless p == "\n" } if min_indent > 0
|
2010-04-01 03:45:16 -04:00
|
|
|
verbatim.normalize
|
|
|
|
|
|
|
|
p :verbatim_end => margin if @debug
|
|
|
|
|
|
|
|
verbatim
|
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# The character offset for the input string at the given +byte_offset+
|
|
|
|
|
|
|
|
def char_pos byte_offset
|
2012-11-29 18:34:29 -05:00
|
|
|
if @have_byteslice then
|
|
|
|
@input.byteslice(0, byte_offset).length
|
|
|
|
elsif @have_encoding then
|
2012-11-26 23:28:14 -05:00
|
|
|
matched = @binary_input[0, byte_offset]
|
|
|
|
matched.force_encoding @input_encoding
|
|
|
|
matched.length
|
|
|
|
else
|
|
|
|
byte_offset
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Pulls the next token from the stream.
|
|
|
|
|
|
|
|
def get
|
|
|
|
@current_token = @tokens.shift
|
|
|
|
p :get => @current_token if @debug
|
|
|
|
@current_token
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
# Parses the tokens into an array of RDoc::Markup::XXX objects,
|
|
|
|
# and appends them to the passed +parent+ RDoc::Markup::YYY object.
|
|
|
|
#
|
|
|
|
# Exits at the end of the token stream, or when it encounters a token
|
|
|
|
# in a column less than +indent+ (unless it is a NEWLINE).
|
|
|
|
#
|
|
|
|
# Returns +parent+.
|
|
|
|
|
|
|
|
def parse parent, indent = 0
|
2010-04-01 03:45:16 -04:00
|
|
|
p :parse_start => indent if @debug
|
|
|
|
|
|
|
|
until @tokens.empty? do
|
|
|
|
type, data, column, = get
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
case type
|
|
|
|
when :BREAK then
|
|
|
|
parent << RDoc::Markup::BlankLine.new
|
|
|
|
skip :NEWLINE, false
|
|
|
|
next
|
|
|
|
when :NEWLINE then
|
2010-12-19 22:22:49 -05:00
|
|
|
# trailing newlines are skipped below, so this is a blank line
|
|
|
|
parent << RDoc::Markup::BlankLine.new
|
|
|
|
skip :NEWLINE, false
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2011-02-01 19:32:30 -05:00
|
|
|
# indentation change: break or verbatim
|
2010-12-19 22:22:49 -05:00
|
|
|
if column < indent then
|
2010-04-01 03:45:16 -04:00
|
|
|
unget
|
|
|
|
break
|
2010-12-19 22:22:49 -05:00
|
|
|
elsif column > indent then
|
|
|
|
unget
|
|
|
|
parent << build_verbatim(indent)
|
|
|
|
next
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
# indentation is the same
|
2010-04-01 03:45:16 -04:00
|
|
|
case type
|
|
|
|
when :HEADER then
|
2010-12-19 22:22:49 -05:00
|
|
|
parent << build_heading(data)
|
2010-04-01 03:45:16 -04:00
|
|
|
when :RULE then
|
2010-12-19 22:22:49 -05:00
|
|
|
parent << RDoc::Markup::Rule.new(data)
|
2010-04-01 03:45:16 -04:00
|
|
|
skip :NEWLINE
|
|
|
|
when :TEXT then
|
|
|
|
unget
|
2013-09-18 19:33:36 -04:00
|
|
|
parse_text parent, indent
|
2010-04-01 03:45:16 -04:00
|
|
|
when *LIST_TOKENS then
|
|
|
|
unget
|
2010-12-19 22:22:49 -05:00
|
|
|
parent << build_list(indent)
|
2010-04-01 03:45:16 -04:00
|
|
|
else
|
|
|
|
type, data, column, line = @current_token
|
2010-12-19 22:22:49 -05:00
|
|
|
raise ParseError, "Unhandled token #{type} (#{data.inspect}) at #{line}:#{column}"
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
p :parse_end => indent if @debug
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
parent
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
##
|
|
|
|
# Small hook that is overridden by RDoc::TomDoc
|
|
|
|
|
|
|
|
def parse_text parent, indent # :nodoc:
|
|
|
|
parent << build_paragraph(indent)
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Returns the next token on the stream without modifying the stream
|
|
|
|
|
|
|
|
def peek_token
|
|
|
|
token = @tokens.first || []
|
|
|
|
p :peek => token if @debug
|
|
|
|
token
|
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# Creates the StringScanner
|
|
|
|
|
|
|
|
def setup_scanner input
|
|
|
|
@line = 0
|
|
|
|
@line_pos = 0
|
2012-11-29 18:34:29 -05:00
|
|
|
@input = input.dup
|
2012-11-26 23:28:14 -05:00
|
|
|
|
2012-11-29 18:34:29 -05:00
|
|
|
if @have_encoding and not @have_byteslice then
|
|
|
|
@input_encoding = @input.encoding
|
|
|
|
@binary_input = @input.force_encoding Encoding::BINARY
|
2012-11-26 23:28:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@s = StringScanner.new input
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
# Skips the next token if its type is +token_type+.
|
|
|
|
#
|
|
|
|
# Optionally raises an error if the next token is not of the expected type.
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
def skip token_type, error = true
|
2010-11-08 15:58:28 -05:00
|
|
|
type, = get
|
2010-04-01 03:45:16 -04:00
|
|
|
return unless type # end of stream
|
|
|
|
return @current_token if token_type == type
|
|
|
|
unget
|
2010-12-19 22:22:49 -05:00
|
|
|
raise ParseError, "expected #{token_type} got #{@current_token.inspect}" if error
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Turns text +input+ into a stream of tokens
|
|
|
|
|
|
|
|
def tokenize input
|
2012-11-26 23:28:14 -05:00
|
|
|
setup_scanner input
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
until @s.eos? do
|
|
|
|
pos = @s.pos
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
# leading spaces will be reflected by the column of the next token
|
|
|
|
# the only thing we loose are trailing spaces at the end of the file
|
2012-11-26 23:28:14 -05:00
|
|
|
next if @s.scan(/ +/)
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
# note: after BULLET, LABEL, etc.,
|
|
|
|
# indent will be the column of the next non-newline token
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
@tokens << case
|
2010-12-19 22:22:49 -05:00
|
|
|
# [CR]LF => :NEWLINE
|
2012-11-26 23:28:14 -05:00
|
|
|
when @s.scan(/\r?\n/) then
|
|
|
|
token = [:NEWLINE, @s.matched, *token_pos(pos)]
|
|
|
|
@line_pos = char_pos @s.pos
|
2010-04-01 03:45:16 -04:00
|
|
|
@line += 1
|
|
|
|
token
|
2010-12-19 22:22:49 -05:00
|
|
|
# === text => :HEADER then :TEXT
|
2012-11-26 23:28:14 -05:00
|
|
|
when @s.scan(/(=+)(\s*)/) then
|
|
|
|
level = @s[1].length
|
2011-07-30 20:19:00 -04:00
|
|
|
header = [:HEADER, level, *token_pos(pos)]
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
if @s[2] =~ /^\r?\n/ then
|
|
|
|
@s.pos -= @s[2].length
|
2011-07-30 20:19:00 -04:00
|
|
|
header
|
|
|
|
else
|
2012-11-26 23:28:14 -05:00
|
|
|
pos = @s.pos
|
|
|
|
@s.scan(/.*/)
|
2011-07-30 20:19:00 -04:00
|
|
|
@tokens << header
|
2012-11-26 23:28:14 -05:00
|
|
|
[:TEXT, @s.matched.sub(/\r$/, ''), *token_pos(pos)]
|
2011-07-30 20:19:00 -04:00
|
|
|
end
|
2010-12-19 22:22:49 -05:00
|
|
|
# --- (at least 3) and nothing else on the line => :RULE
|
2012-11-26 23:28:14 -05:00
|
|
|
when @s.scan(/(-{3,}) *\r?$/) then
|
|
|
|
[:RULE, @s[1].length - 2, *token_pos(pos)]
|
2010-12-19 22:22:49 -05:00
|
|
|
# * or - followed by white space and text => :BULLET
|
2012-11-26 23:28:14 -05:00
|
|
|
when @s.scan(/([*-]) +(\S)/) then
|
|
|
|
@s.pos -= @s[2].bytesize # unget \S
|
|
|
|
[:BULLET, @s[1], *token_pos(pos)]
|
2010-12-19 22:22:49 -05:00
|
|
|
# A. text, a. text, 12. text => :UALPHA, :LALPHA, :NUMBER
|
2012-11-26 23:28:14 -05:00
|
|
|
when @s.scan(/([a-z]|\d+)\. +(\S)/i) then
|
2010-12-19 22:22:49 -05:00
|
|
|
# FIXME if tab(s), the column will be wrong
|
|
|
|
# either support tabs everywhere by first expanding them to
|
|
|
|
# spaces, or assume that they will have been replaced
|
|
|
|
# before (and provide a check for that at least in debug
|
|
|
|
# mode)
|
2012-11-26 23:28:14 -05:00
|
|
|
list_label = @s[1]
|
|
|
|
@s.pos -= @s[2].bytesize # unget \S
|
2010-12-19 22:22:49 -05:00
|
|
|
list_type =
|
|
|
|
case list_label
|
|
|
|
when /[a-z]/ then :LALPHA
|
|
|
|
when /[A-Z]/ then :UALPHA
|
|
|
|
when /\d/ then :NUMBER
|
|
|
|
else
|
|
|
|
raise ParseError, "BUG token #{list_label}"
|
|
|
|
end
|
|
|
|
[list_type, list_label, *token_pos(pos)]
|
|
|
|
# [text] followed by spaces or end of line => :LABEL
|
2012-11-26 23:28:14 -05:00
|
|
|
when @s.scan(/\[(.*?)\]( +|\r?$)/) then
|
|
|
|
[:LABEL, @s[1], *token_pos(pos)]
|
2010-12-19 22:22:49 -05:00
|
|
|
# text:: followed by spaces or end of line => :NOTE
|
2012-11-26 23:28:14 -05:00
|
|
|
when @s.scan(/(.*?)::( +|\r?$)/) then
|
|
|
|
[:NOTE, @s[1], *token_pos(pos)]
|
2010-12-19 22:22:49 -05:00
|
|
|
# anything else: :TEXT
|
2012-11-26 23:28:14 -05:00
|
|
|
else @s.scan(/(.*?)( )?\r?$/)
|
|
|
|
token = [:TEXT, @s[1], *token_pos(pos)]
|
|
|
|
|
|
|
|
if @s[2] then
|
|
|
|
@tokens << token
|
|
|
|
[:BREAK, @s[2], *token_pos(pos + @s[1].length)]
|
|
|
|
else
|
|
|
|
token
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2013-11-18 19:47:33 -05:00
|
|
|
# Calculates the column (by character) and line of the current token based
|
|
|
|
# on +byte_offset+.
|
2012-11-26 23:28:14 -05:00
|
|
|
|
|
|
|
def token_pos byte_offset
|
|
|
|
offset = char_pos byte_offset
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
[offset - @line_pos, @line]
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Returns the current token to the token stream
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
def unget
|
|
|
|
token = @current_token
|
2010-04-01 03:45:16 -04:00
|
|
|
p :unget => token if @debug
|
|
|
|
raise Error, 'too many #ungets' if token == @tokens.first
|
|
|
|
@tokens.unshift token if token
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|