Class: Radius::Scanner

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

Direct Known Subclasses

SquiggleScanner

Instance Method Summary collapse

Instance Method Details

#operate(prefix, data) ⇒ Object

Parses a given string and returns an array of nodes. The nodes consist of strings and hashes that describe a Radius tag that was found.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/radius/parser/scanner.rb', line 12

def operate(prefix, data)
  data = Radius::OrdString.new data
  @nodes = ['']
  
  re = scanner_regex(prefix)
  if md = re.match(data)      
    remainder = ''  
    while md
      start_tag, attributes, self_enclosed, end_tag = $1, $2, $3, $4

      flavor = self_enclosed == '/' ? :self : (start_tag ? :open : :close)
      
      # save the part before the current match as a string node
      @nodes << md.pre_match
      
      # save the tag that was found as a tag hash node
      @nodes << {:prefix=>prefix, :name=>(start_tag || end_tag), :flavor => flavor, :attrs => parse_attributes(attributes)}
      
      # remember the part after the current match
      remainder = md.post_match
      
      # see if we find another tag in the remaining string
      md = re.match(md.post_match)
    end  
    
    # add the last remaining string after the last tag that was found as a string node
    @nodes << remainder
  else
    @nodes << data
  end

  return @nodes
end

#scanner_regex(prefix = nil) ⇒ Object

The regular expression used to find (1) opening and self-enclosed tag names, (2) self-enclosing trailing slash, (3) attributes and (4) closing tag



6
7
8
# File 'lib/radius/parser/scanner.rb', line 6

def scanner_regex(prefix = nil)
  %r{<#{prefix}:([\w:]+?)(\s+(?:\w+\s*=\s*(?:"[^"]*?"|'[^']*?')\s*)*|)(\/?)>|<\/#{prefix}:([\w:]+?)\s*>}
end