Class: Postsvg::Tokenizer

Inherits:
Object
  • Object
show all
Defined in:
lib/postsvg/tokenizer.rb

Overview

Tokenizes PostScript code into tokens

Class Method Summary collapse

Class Method Details

.match_hex_string(ps, index) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/postsvg/tokenizer.rb', line 142

def self.match_hex_string(ps, index)
  i = index + 1
  hex_content = +""

  while i < ps.length && ps[i] != ">"
    hex_content << ps[i] unless ps[i].match?(/\s/)
    i += 1
  end

  # Convert hex to string
  str = +""
  (0...hex_content.length).step(2) do |j|
    byte = hex_content[j, 2]
    str << byte.to_i(16).chr
  end

  [Token.new(type: "hexstring", value: str), i + 1]
end

.match_string(ps, index) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/postsvg/tokenizer.rb', line 79

def self.match_string(ps, index)
  result = +""
  i = index + 1 # Skip opening (
  depth = 1

  while i < ps.length && depth.positive?
    case ps[i]
    when "\\"
      # Escape sequence
      i += 1
      break if i >= ps.length

      case ps[i]
      when "n" then result << "\n"
      when "r" then result << "\r"
      when "t" then result << "\t"
      when "b" then result << "\b"
      when "f" then result << "\f"
      when "(" then result << "("
      when ")" then result << ")"
      when "\\" then result << "\\"
      when " " then result << " "
      else
        # Octal: \ddd
        if ps[i].between?("0", "7")
          octal = ps[i]
          i += 1
          if i < ps.length && ps[i] >= "0" && ps[i] <= "7"
            octal += ps[i]
            i += 1
            if i < ps.length && ps[i] >= "0" && ps[i] <= "7"
              octal += ps[i]
              i += 1
            end
          end
          code = octal.to_i(8)
          code = 255 if code > 255
          result << code.chr
          i -= 1 # Will be incremented at end of loop
        else
          result << ps[i]
        end
      end
    when "("
      depth += 1
      result << ps[i]
    when ")"
      depth -= 1
      if depth.zero?
        return [Token.new(type: "string", value: result),
                i + 1]
      end

      result << ps[i]
    else
      result << ps[i]
    end
    i += 1
  end

  [Token.new(type: "string", value: result), i]
end

.match_token(ps, index) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/postsvg/tokenizer.rb', line 35

def self.match_token(ps, index)
  # String: (foo) or (a\)b)
  return match_string(ps, index) if ps[index] == "("

  # Number: 12 .2 3e4
  if (match = ps[index..].match(/\A-?(?:\d+\.\d+|\d+\.|\.\d+|\d+)(?:[eE][+-]?\d+)?/))
    return [Token.new(type: "number", value: match[0]),
            index + match[0].length]
  end

  # Braces: { }
  return [Token.new(type: "brace", value: ps[index]), index + 1] if ["{",
                                                                     "}"].include?(ps[index])

  # Brackets: [ ]
  return [Token.new(type: "bracket", value: ps[index]), index + 1] if ["[",
                                                                       "]"].include?(ps[index])

  # Dict markers: << >>
  return [Token.new(type: "dict", value: ps[index, 2]), index + 2] if [
    "<<", ">>"
  ].include?(ps[index, 2])

  # Hex strings: <...>
  if ps[index] == "<" && ps[index + 1] != "<"
    return match_hex_string(ps,
                            index)
  end

  # Names/Operators: /foo or foo
  if (match = ps[index..].match(%r{\A/?[A-Za-z_\-.?*][A-Za-z0-9_\-.?*]*}))
    value = match[0]
    if value.start_with?("/")
      return [Token.new(type: "name", value: value[1..]),
              index + value.length]
    end

    return [Token.new(type: "operator", value: value), index + value.length]

  end

  nil
end

.tokenize(ps_code) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/postsvg/tokenizer.rb', line 9

def self.tokenize(ps_code)
  # Remove comments
  ps = ps_code.gsub(/%[^\n\r]*/, " ")

  tokens = []
  index = 0

  while index < ps.length
    # Skip whitespace
    index += 1 while index < ps.length && ps[index].match?(/\s/)
    break if index >= ps.length

    # Try to match different token types
    token, new_index = match_token(ps, index)
    if token
      tokens << token
      index = new_index
    else
      # Skip invalid character
      index += 1
    end
  end

  tokens
end