Class: Jekyll::Premonition::Attributes::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/premonition/attributes/parser.rb

Overview

Public: Premonition block attributes parser.

Parses attributes found in block headers like this: > “info” [ foo = abcd, bar = “zot”, size = 3 ]

The parser itself utilizes the StringScanner in ruby. Each character will be parsed, validated and pushed to a stack (array) according to the rules inside the parser itself.

A stack object will be of a certain type:

0 : Outside an attributes block 1 : Inside an attributes block, but between keys or values 2 : Parsing an attribute key 3: Parsing an attribute value

Upon parser errors a pretty syntax error will be printed to stdout, showing where the error is.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Parser

Initialize a new Parser AND start parsing

str - A string containing the attributes block to be parser.



34
35
36
37
38
39
40
# File 'lib/premonition/attributes/parser.rb', line 34

def initialize(str)
  @raw = str                       # Keeps th original string for later use
  @buffer = StringScanner.new(str) # Create StringScanner buffer
  @attributes = {}                 # Initialize the attributes hash
  @stack = [Stacker.new(0)]        # Initialize the parser stack with initial "state"
  parse                            # Start parsing
end

Instance Attribute Details

#attributesObject (readonly)

Get parsed attributes as a Hash



29
30
31
# File 'lib/premonition/attributes/parser.rb', line 29

def attributes
  @attributes
end