Class: Curly::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/curly/scanner.rb

Overview

Scans Curly templates for tokens.

The Scanner goes through the template piece by piece, extracting tokens until the end of the template is reached.

Constant Summary collapse

CURLY_START =
/\{\{/
CURLY_END =
/\}\}/
ESCAPED_CURLY_START =
/\{\{\{/
COMMENT_MARKER =
/!/
CONTEXT_BLOCK_MARKER =
/@/
CONDITIONAL_BLOCK_MARKER =
/#/
INVERSE_BLOCK_MARKER =
/\^/
COLLECTION_BLOCK_MARKER =
/\*/
END_BLOCK_MARKER =
/\//

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Scanner

Returns a new instance of Scanner



40
41
42
# File 'lib/curly/scanner.rb', line 40

def initialize(source)
  @scanner = StringScanner.new(source)
end

Class Method Details

.scan(source) ⇒ Object

Scans a Curly template for tokens.

source - The String source of the template.

Examples

Curly::Scanner.scan("hello {name}!") #=> [[:text, "hello "], [:component, "name"], [:text, "!"]]

Returns an Array of type/value pairs representing the tokens in the template.



36
37
38
# File 'lib/curly/scanner.rb', line 36

def self.scan(source)
  new(source).scan
end

Instance Method Details

#scanObject



44
45
46
47
48
# File 'lib/curly/scanner.rb', line 44

def scan
  tokens = []
  tokens << scan_token until @scanner.eos?
  tokens
end