Package org.metricshub.jawk
Class Awk
java.lang.Object
org.metricshub.jawk.Awk
- Direct Known Subclasses:
SandboxedAwk
Entry point into the parsing, analysis, and execution
of a Jawk script.
This entry point is used both when Jawk is executed as a library and when
invoked from the command line.
The overall process to execute a Jawk script is as follows:
- Parse the Jawk script, producing an abstract syntax tree.
- Traverse the abstract syntax tree, producing a list of instruction tuples for the interpreter.
- Traverse the list of tuples, providing a runtime which ultimately executes the Jawk script, or Command-line parameters dictate which action is to take place.
The engine does not enable any extensions automatically. Extensions can be
provided programmatically via the Awk(Collection) constructors or
via the command line when using the CLI entry point.
- Author:
- Danny Daglas
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionAwk()Create a new instance of Awk without extensionsAwk(Collection<? extends JawkExtension> extensions) Create a new instance of Awk with the specified extension instances.Awk(JawkExtension... extensions) Create a new instance of Awk with the specified extension instances. -
Method Summary
Modifier and TypeMethodDescriptionCompiles the specified AWK script and returns the intermediate representation asAwkTuples.Compiles the specified AWK script and returns the intermediate representation asAwkTuples.compile(List<ScriptSource> scripts) compileForEval(String expression) Compile an expression to evaluate (not a full script).Evaluates the specified AWK expression (not a full script, just an expression) and returns the value of this expression.Evaluates the specified AWK expression (not a full script, just an expression) and returns the value of this expression.Evaluates the specified AWK expression (not a full script, just an expression) and returns the value of this expression.Evaluates the specified AWK tuples, i.e. the result of the execution of the TERNARY_EXPRESSION AST (the value that has been pushed in the stack).Returns the last parsed AST produced bycompile(List).voidinvoke(String script, AwkSettings settings) invoke.voidinvoke(List<ScriptSource> scripts, AwkSettings settings) Compiles and invokes the specified list ofScriptSources using the providedAwkSettings.voidinvoke(AwkTuples tuples, AwkSettings settings) Interprets the specified precompiledAwkTuplesusing the providedAwkSettings.voidinvoke(ScriptSource script, AwkSettings settings) Compiles and invokes a singleScriptSourceusing the providedAwkSettings.static Map<String,JawkExtension> Lists metadata for theJawkExtensionimplementations discovered on the class path.Executes the specified AWK script against the given input file and returns the printed output as aString.voidrun(Reader script, File input, OutputStream output) Executes the specified AWK script against the given input file and writes the printed output to the providedOutputStream.run(Reader script, InputStream input) Executes the specified AWK script against the provided input stream and returns the printed output as aString.voidrun(Reader script, InputStream input, OutputStream output) Executes the specified AWK script against the provided input stream and writes the result to the givenOutputStream.Executes the specified AWK script against the given input and returns the printed output as aString.voidrun(Reader script, Reader input, OutputStream output) Executes the specified AWK script against the given input and writes the result to the providedOutputStream.Executes the specified AWK script against the given input and returns the printed output as aString.voidrun(Reader script, String input, OutputStream output) Executes the specified AWK script against the given input and writes the result to the providedOutputStream.Executes the specified AWK script against the given input file and returns the printed output as aString.voidrun(String script, File input, OutputStream output) Executes the specified AWK script against the given input file and writes the printed output to the providedOutputStream.run(String script, InputStream input) Executes the specified AWK script against the provided input stream and returns the printed output as aString.voidrun(String script, InputStream input, OutputStream output) Executes the specified AWK script against the provided input stream and writes the result to the givenOutputStream.Executes the specified AWK script against the given input and returns the printed output as aString.voidrun(String script, Reader input, OutputStream output) Executes the specified AWK script against the given input and writes the result to the providedOutputStream.Executes the specified AWK script against the given input and returns the printed output as aString.voidrun(String script, String input, OutputStream output) Executes the specified AWK script against the given input and writes the result to the providedOutputStream.
-
Constructor Details
-
Awk
public Awk()Create a new instance of Awk without extensions -
Awk
Create a new instance of Awk with the specified extension instances.- Parameters:
extensions- extension instances implementingJawkExtension
-
Awk
Create a new instance of Awk with the specified extension instances.- Parameters:
extensions- extension instances implementingJawkExtension
-
-
Method Details
-
getLastAst
Returns the last parsed AST produced bycompile(List).- Returns:
- the last
AstNode, ornullif no compilation occurred
-
invoke
public void invoke(String script, AwkSettings settings) throws IOException, ClassNotFoundException, ExitException invoke.
- Parameters:
settings- This tells AWK what to do (where to get input from, where to write it to, in what mode to run, ...)- Throws:
IOException- upon an IO error.ClassNotFoundException- if intermediate code is specified but deserialization fails to load in the JVMExitException- if interpretation is requested, and a specific exit code is requested.
-
invoke
public void invoke(ScriptSource script, AwkSettings settings) throws IOException, ClassNotFoundException, ExitException Compiles and invokes a singleScriptSourceusing the providedAwkSettings. This is a convenience overload for callers who have a single script to execute.- Parameters:
script- script source to compile and runsettings- runtime settings such as input and output streams- Throws:
IOException- if an I/O error occurs during compilation or executionClassNotFoundException- if intermediate code cannot be loadedExitException- if the script terminates with a non-zero exit code
-
invoke
public void invoke(List<ScriptSource> scripts, AwkSettings settings) throws IOException, ClassNotFoundException, ExitException Compiles and invokes the specified list ofScriptSources using the providedAwkSettings.- Parameters:
scripts- list of script sources to compile and runsettings- runtime settings such as input and output streams- Throws:
IOException- if an I/O error occurs during compilation or executionClassNotFoundException- if intermediate code cannot be loadedExitException- if the script terminates with a non-zero exit code
-
invoke
Interprets the specified precompiledAwkTuplesusing the providedAwkSettings.- Parameters:
tuples- precompiled tuples to interpretsettings- runtime settings- Throws:
IOException- upon an IO errorExitException- if interpretation is requested, and a specific exit code is requested
-
run
public String run(String script, String input) throws IOException, ClassNotFoundException, ExitException Executes the specified AWK script against the given input and returns the printed output as aString.- Parameters:
script- AWK script to executeinput- text to process- Returns:
- result of the execution as a String
- Throws:
IOException- if an I/O error occursClassNotFoundException- if intermediate code cannot be loadedExitException- if the script terminates with a non-zero exit code
-
run
public void run(String script, String input, OutputStream output) throws IOException, ClassNotFoundException, ExitException Executes the specified AWK script against the given input and writes the result to the providedOutputStream.- Parameters:
script- AWK script to executeinput- text to processoutput- destination for the printed output- Throws:
IOException- if an I/O error occursClassNotFoundException- if intermediate code cannot be loadedExitException- if the script terminates with a non-zero exit code
-
run
public String run(Reader script, String input) throws IOException, ClassNotFoundException, ExitException Executes the specified AWK script against the given input and returns the printed output as aString.- Parameters:
script- AWK script to execute (as aReader)input- text to process- Returns:
- result of the execution as a String
- Throws:
IOException- if an I/O error occursClassNotFoundException- if intermediate code cannot be loadedExitException- if the script terminates with a non-zero exit code
-
run
public void run(Reader script, String input, OutputStream output) throws IOException, ClassNotFoundException, ExitException Executes the specified AWK script against the given input and writes the result to the providedOutputStream.- Parameters:
script- AWK script to execute (as aReader)input- text to processoutput- destination for the printed output- Throws:
IOException- if an I/O error occursClassNotFoundException- if intermediate code cannot be loadedExitException- if the script terminates with a non-zero exit code
-
run
public String run(String script, Reader input) throws IOException, ClassNotFoundException, ExitException Executes the specified AWK script against the given input and returns the printed output as aString.- Parameters:
script- AWK script to executeinput- text reader to process- Returns:
- result of the execution as a String
- Throws:
IOException- if an I/O error occursClassNotFoundException- if intermediate code cannot be loadedExitException- if the script terminates with a non-zero exit code
-
run
public void run(String script, Reader input, OutputStream output) throws IOException, ClassNotFoundException, ExitException Executes the specified AWK script against the given input and writes the result to the providedOutputStream.- Parameters:
script- AWK script to executeinput- text reader to processoutput- destination for the printed output- Throws:
IOException- if an I/O error occursClassNotFoundException- if intermediate code cannot be loadedExitException- if the script terminates with a non-zero exit code
-
run
public String run(Reader script, Reader input) throws IOException, ClassNotFoundException, ExitException Executes the specified AWK script against the given input and returns the printed output as aString.- Parameters:
script- AWK script to execute (as aReader)input- text reader to process- Returns:
- result of the execution as a String
- Throws:
IOException- if an I/O error occursClassNotFoundException- if intermediate code cannot be loadedExitException- if the script terminates with a non-zero exit code
-
run
public void run(Reader script, Reader input, OutputStream output) throws IOException, ClassNotFoundException, ExitException Executes the specified AWK script against the given input and writes the result to the providedOutputStream.- Parameters:
script- AWK script to execute (as aReader)input- text reader to processoutput- destination for the printed output- Throws:
IOException- if an I/O error occursClassNotFoundException- if intermediate code cannot be loadedExitException- if the script terminates with a non-zero exit code
-
run
public String run(String script, File input) throws IOException, ClassNotFoundException, ExitException Executes the specified AWK script against the given input file and returns the printed output as aString.- Parameters:
script- AWK script to executeinput- file containing text to process- Returns:
- result of the execution as a String
- Throws:
IOException- if an I/O error occursClassNotFoundException- if intermediate code cannot be loadedExitException- if the script terminates with a non-zero exit code
-
run
public void run(String script, File input, OutputStream output) throws IOException, ClassNotFoundException, ExitException Executes the specified AWK script against the given input file and writes the printed output to the providedOutputStream.- Parameters:
script- AWK script to executeinput- file containing text to processoutput- destination for the printed output- Throws:
IOException- if an I/O error occursClassNotFoundException- if intermediate code cannot be loadedExitException- if the script terminates with a non-zero exit code
-
run
public String run(Reader script, File input) throws IOException, ClassNotFoundException, ExitException Executes the specified AWK script against the given input file and returns the printed output as aString.- Parameters:
script- AWK script to execute (as aReader)input- file containing text to process- Returns:
- result of the execution as a String
- Throws:
IOException- if an I/O error occursClassNotFoundException- if intermediate code cannot be loadedExitException- if the script terminates with a non-zero exit code
-
run
public void run(Reader script, File input, OutputStream output) throws IOException, ClassNotFoundException, ExitException Executes the specified AWK script against the given input file and writes the printed output to the providedOutputStream.- Parameters:
script- AWK script to execute (as aReader)input- file containing text to processoutput- destination for the printed output- Throws:
IOException- if an I/O error occursClassNotFoundException- if intermediate code cannot be loadedExitException- if the script terminates with a non-zero exit code
-
run
public String run(String script, InputStream input) throws IOException, ClassNotFoundException, ExitException Executes the specified AWK script against the provided input stream and returns the printed output as aString.- Parameters:
script- AWK script to executeinput- stream to process- Returns:
- result of the execution as a String
- Throws:
IOException- if an I/O error occursClassNotFoundException- if intermediate code cannot be loadedExitException- if the script terminates with a non-zero exit code
-
run
public void run(String script, InputStream input, OutputStream output) throws IOException, ClassNotFoundException, ExitException Executes the specified AWK script against the provided input stream and writes the result to the givenOutputStream.- Parameters:
script- AWK script to executeinput- stream to processoutput- destination for the printed output- Throws:
IOException- if an I/O error occursClassNotFoundException- if intermediate code cannot be loadedExitException- if the script terminates with a non-zero exit code
-
run
public String run(Reader script, InputStream input) throws IOException, ClassNotFoundException, ExitException Executes the specified AWK script against the provided input stream and returns the printed output as aString.- Parameters:
script- AWK script to execute (as aReader)input- stream to process- Returns:
- result of the execution as a String
- Throws:
IOException- if an I/O error occursClassNotFoundException- if intermediate code cannot be loadedExitException- if the script terminates with a non-zero exit code
-
run
public void run(Reader script, InputStream input, OutputStream output) throws IOException, ClassNotFoundException, ExitException Executes the specified AWK script against the provided input stream and writes the result to the givenOutputStream.- Parameters:
script- AWK script to execute (as aReader)input- stream to processoutput- destination for the printed output- Throws:
IOException- if an I/O error occursClassNotFoundException- if intermediate code cannot be loadedExitException- if the script terminates with a non-zero exit code
-
compile
Compiles the specified AWK script and returns the intermediate representation asAwkTuples.- Parameters:
script- AWK script to compile- Returns:
- compiled
AwkTuples - Throws:
IOException- if an I/O error occurs during compilation
-
compile
Compiles the specified AWK script and returns the intermediate representation asAwkTuples.- Parameters:
script- AWK script to compile (as aReader)- Returns:
- compiled
AwkTuples - Throws:
IOException- if an I/O error occurs during compilation
-
compile
- Parameters:
scripts- script sources to compile- Returns:
- compiled
AwkTuples - Throws:
IOException- if an I/O error occurs while reading the scripts
-
compileForEval
Compile an expression to evaluate (not a full script).- Parameters:
expression- AWK expression to compile to AwkTuples- Returns:
- AwkTuples to be interpreted by AVM
- Throws:
IOException- if anything goes wrong with the compilation
-
eval
Evaluates the specified AWK expression (not a full script, just an expression) and returns the value of this expression.- Parameters:
expression- Expression to evaluate (e.g.2+3)- Returns:
- the value of the specified expression
- Throws:
IOException- if anything goes wrong with the evaluation
-
eval
Evaluates the specified AWK expression (not a full script, just an expression) and returns the value of this expression.- Parameters:
expression- Expression to evaluate (e.g.2+3or$2 "-" $3input- Optional text input (that will be available as $0, and tokenized as $1, $2, etc.)- Returns:
- the value of the specified expression
- Throws:
IOException- if anything goes wrong with the evaluation
-
eval
Evaluates the specified AWK expression (not a full script, just an expression) and returns the value of this expression.- Parameters:
expression- Expression to evaluate (e.g.2+3or$2 "-" $3input- Optional text input (that will be available as $0, and tokenized as $1, $2, etc.)fieldSeparator- Value of the FS global variable used for parsing the input- Returns:
- the value of the specified expression
- Throws:
IOException- if anything goes wrong with the evaluation
-
eval
Evaluates the specified AWK tuples, i.e. the result of the execution of the TERNARY_EXPRESSION AST (the value that has been pushed in the stack).- Parameters:
tuples- Tuples returned bycompileForEval(String)input- Optional text input (that will be available as $0, and tokenized as $1, $2, etc.)fieldSeparator- Value of the FS global variable used for parsing the input- Returns:
- the value of the specified expression
- Throws:
IOException- if anything goes wrong with the evaluation
-
listAvailableExtensions
Lists metadata for theJawkExtensionimplementations discovered on the class path.- Returns:
- list of discovered extension descriptors
-