From aeba0760db69b9ff2f02f9c83101eff4c38cd0c5 Mon Sep 17 00:00:00 2001 From: Henri DF Date: Sun, 14 Feb 2016 14:31:10 -0800 Subject: [PATCH] Scaffold basic line-oriented compiler API --- lua/sysdig-parser.lua | 44 ++++++++++++++++++++++++++++++++++++------- lua/test.lua | 6 ++++-- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/lua/sysdig-parser.lua b/lua/sysdig-parser.lua index 720eac54..a68b9e50 100644 --- a/lua/sysdig-parser.lua +++ b/lua/sysdig-parser.lua @@ -13,7 +13,8 @@ --]] -local parser = {} +local compiler = {} +compiler.parser = {} local lpeg = require "lpeg" @@ -340,15 +341,44 @@ end -function parser.parse (subject) +--[[ + Parses a single line (which should be either a macro definition or a filter) and returns the AST. +--]] +function compiler.parser.parseline (subject) local errorinfo = { subject = subject } lpeg.setmaxstack(1000) local ast, error_msg = lpeg.match(G, subject, nil, errorinfo) - if (error_msg) then - return ast, error_msg - end - expand_in(ast) return ast, error_msg end -return parser + +--[[ + Sets up compiler state and returns it. + + This is an opaque blob that is passed into subsequent compiler calls and + should not be modified by the client. + + It holds state such as macro definitions that must be kept across calls + to the line-oriented compiler. +--]] +function compiler.init() + return {} +end + +--[[ + Compiles a digwatch filter or macro +--]] +function compiler.compile_line(line, state) + ast, error_message = compiler.parser.parseline(line) + + if (error_msg) then + return {}, state, error_msg + end + expand_in(ast) +-- extract_macros(ast, state) +-- expand_macros(ast, state) + return ast, state, error_msg +end + + +return compiler diff --git a/lua/test.lua b/lua/test.lua index a34427b1..9c50d724 100644 --- a/lua/test.lua +++ b/lua/test.lua @@ -1,11 +1,13 @@ -local parser = require "sysdig-parser" +local compiler = require "sysdig-parser" if #arg ~= 1 then print("Usage: test.lua ") os.exit(1) end -local ast, error_msg = parser.parse(arg[1]) +local state = compiler.init() + +local ast, state, error_msg = compiler.compile_line(arg[1], state) if not ast then os.exit(1) end