class SimpleTemplates::Parser::Placeholder

Recognizes a set of input tokens as a Placeholder.

Constants

EXPECTED_TAG_ORDER

The expected tag order for a valid placeholder.

STARTING_TOKENS

The starting token that the input must have

Public Instance Methods

parse() click to toggle source

If this stream starts with a placeholder token, parse out the Placeholder, or a Result with errors indicating the syntax problem. @return <Array <Array>,

<Array[SimpleTemplates::Parser::Error]>,
<Array[SimpleTemplates::Lexer::Token]>> an +Array+ with the
AST::Placeholder as first element, a list of parser errors and a list
of the remaining tokens.
# File lib/simple_templates/parser/placeholder.rb, line 24
def parse
  errors = check_placeholder_syntax

  remaining_tokens = []

  placeholder_ast = if errors.empty?
    remaining_tokens = tokens[3..-1] || []

    allowed = allowed_placeholders.nil? ||
              allowed_placeholders.include?(tag_name.content)

    [AST::Placeholder.new(tag_name.content, tag_start.pos, allowed)]
  else
    [] # we don't have an AST portion to return if we encountered errors
  end

  [placeholder_ast, errors, remaining_tokens]
end

Private Instance Methods

check_placeholder_syntax() click to toggle source
# File lib/simple_templates/parser/placeholder.rb, line 45
def check_placeholder_syntax
  expected_order_with_found_tokens = EXPECTED_TAG_ORDER.zip(tag_tokens)

  errors = expected_order_with_found_tokens.
             reduce([]) do |errs, (expected_type, found_tag)|

    if found_tag.nil?
      break errs << Parser::Error.new(
        "Expected #{FRIENDLY_TAG_NAMES.fetch(expected_type)} token, but" +
        " reached end of input.")

    elsif expected_type != found_tag.type
      break errs << Parser::Error.new(
        "Expected #{FRIENDLY_TAG_NAMES.fetch(expected_type)} token at " +
        "character position #{found_tag.pos}, but found a " +
        "#{FRIENDLY_TAG_NAMES.fetch(found_tag.type)} token instead.")

    else
      # This token was expected at this point in the placeholder sequence,
      # no need to add errors.
      errs
    end
  end
end
tag_name() click to toggle source
# File lib/simple_templates/parser/placeholder.rb, line 78
def tag_name
  tokens[1]
end
tag_start() click to toggle source
# File lib/simple_templates/parser/placeholder.rb, line 74
def tag_start
  tokens[0]
end
tag_tokens() click to toggle source
# File lib/simple_templates/parser/placeholder.rb, line 70
def tag_tokens
  tokens[0..2].compact
end