From 571fce2672c9d42b2c7ad6d8701d2a9f1821c022 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 6 Apr 2018 09:20:11 -0400 Subject: [PATCH] Begin using whittle to parse preprocessor output --- lib/cxl/cxl/lib/cxl.rb | 3 +-- lib/cxl/cxl/lib/cxl/parser.rb | 38 ++++++++++++++++++++++------------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/lib/cxl/cxl/lib/cxl.rb b/lib/cxl/cxl/lib/cxl.rb index 31180bf..000a306 100644 --- a/lib/cxl/cxl/lib/cxl.rb +++ b/lib/cxl/cxl/lib/cxl.rb @@ -7,8 +7,7 @@ module Cxl class << self def compile_to(input_fname, output_fname, cppflags) - parser = Parser.new(input_fname, cppflags) - parsed = parser.parse + parsed = Parser.load(input_fname, cppflags) 0 end diff --git a/lib/cxl/cxl/lib/cxl/parser.rb b/lib/cxl/cxl/lib/cxl/parser.rb index a247130..9a968d3 100644 --- a/lib/cxl/cxl/lib/cxl/parser.rb +++ b/lib/cxl/cxl/lib/cxl/parser.rb @@ -1,24 +1,34 @@ require "open3" +require "whittle" module Cxl - class Parser + class Parser < Whittle::Parser + rule(:tok => /./) + rule(:eol => "\n") - def initialize(fname, cppflags) - @fname = fname - @cppflags = cppflags + rule(:translation_unit) do |r| + r[] + r[:translation_unit, :tok] + r[:translation_unit, :eol] end - def parse - c_preprocessor_command = Config.instance.c_preprocessor - command = c_preprocessor_command + @cppflags + [@fname] - pp_out, pp_err, status = Open3.capture3(*command) - if pp_err != "" - $stderr.write(pp_err) + start(:translation_unit) + + class << self + + def load(fname, cppflags) + c_preprocessor_command = Config.instance.c_preprocessor + command = c_preprocessor_command + cppflags + [fname] + pp_out, pp_err, status = Open3.capture3(*command) + if pp_err != "" + $stderr.write(pp_err) + end + if status != 0 + return + end + Parser.new.parse(pp_out) end - if status != 0 - return - end - puts pp_out.lines[0, 2] + end end