File: preprocess_parser.rb

package info (click to toggle)
jruby 9.4.8.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 89,244 kB
  • sloc: ruby: 548,574; java: 276,189; yacc: 25,873; ansic: 6,178; xml: 6,111; sh: 1,855; sed: 94; makefile: 78; jsp: 48; tcl: 40; exp: 12
file content (41 lines) | stat: -rw-r--r-- 1,024 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
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
34
35
36
37
38
39
40
41

# pass in any argument to assume we are processing for ripper instead
# of the main parser.
IS_RIPPER=!!ARGV.shift

def read_line
  line = gets
  return nil unless line
  
  line.gsub(/@@([^@]+)@@/) do |word|
    sub = SUBS[$1]
    raise ArgumentError, "Cannot find substitution for #{word}" unless sub
    sub[IS_RIPPER ? 1 : 0]
  end
end

# We define substitutions at the top of the file where a constant
# named SUBS contains a key which represents a subtition and two value
# where the first value is what is substituted when writing the Parser
# and the second value is what is substituted when writing Ripper Parser.
#
# Any reference to @@name@@ will be replaced with first or second value
# later on in the grammar file.
def read_text_substitutions
  code = ''
  while line = read_line
    break if line =~ %r{^=@@\*/}
    code << line
  end
  # Reads in substitions into the constant SUBS
  eval code
end

while line = read_line
  if line =~ %r{^/\*@@=}
    read_text_substitutions
  else
    puts line
  end
end