Class Awk
- Direct Known Subclasses:
SandboxedAwk
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionfinal classFluent builder for configuring and executing an AWK script or program. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringPOSIX default number-to-string conversion format ("%.6g").static final StringPOSIX default field separator (" ").static final StringPOSIX default output number format ("%.6g").static final StringPOSIX default output field separator (" ").static final StringPOSIX default output record separator ("\n").static final StringPOSIX default record separator ("\n").static final StringPOSIX default subscript separator ("\034"). -
Constructor Summary
ConstructorsConstructorDescriptionAwk()Create a new instance of Awk without extensions.Awk(JawkExtension... extensions) Create a new instance of Awk with the specified extension instances.Awk(AwkSettings settings) Create a new instance of Awk with the specified settings.Awk(Collection<? extends JawkExtension> extensions) Create a new instance of Awk with the specified extension instances.Awk(Collection<? extends JawkExtension> extensions, AwkSettings settings) Create a new instance of Awk with the specified extension instances and settings. -
Method Summary
Modifier and TypeMethodDescriptionCompiles a full AWK program.Compiles a full AWK program.compile(List<ScriptSource> scripts) Compiles a list of script sources into an immutable AWK program that can be executed by theAVMruntime.compile(List<ScriptSource> scripts, boolean disableOptimizeParam) Compiles a list of script sources into an immutable AWK program that can be executed by theAVMruntime.compileExpression(String expression) Compile an expression to evaluate (not a full script).compileExpression(String expression, boolean disableOptimizeParam) Compile an expression to evaluate (not a full script).Creates a reusable runtime backed by oneAVMinstance.createAvm(boolean profilingEnabled) Creates a reusable runtime backed by oneAVMinstance, optionally collecting runtime profiling statistics.eval(AwkExpression expression) Evaluates a compiled expression using a fresh isolated runtime.eval(AwkExpression expression, InputSource source) Evaluates a compiled expression against one structured record source using a fresh isolated runtime.eval(AwkExpression expression, String input) Evaluates a compiled expression against one text record using a fresh isolated runtime.Evaluates the specified AWK expression (not a full script, just an expression) and returns the value of this expression.eval(String expression, InputSource source) Evaluates the specified AWK expression (not a full script, just an expression) and returns the value of this expression.Returns the last parsed AST produced by the most recent program compilation.Returns the behavioral settings associated with this engine instance.static Map<String,JawkExtension> Lists metadata for theJawkExtensionimplementations discovered on the class path.prepareEval(InputSource source) Prepares the first available record from a structuredInputSourcefor repeated expression evaluation and returns the mutableAVMthat will execute those expressions.prepareEval(String input) Prepares one text record for repeated expression evaluation and returns the mutableAVMthat will execute those expressions.script(AwkProgram program) Starts building a run request for a compiled AWK program.Starts building a run request from an AWK script string.
-
Field Details
-
DEFAULT_FS
POSIX default field separator (" ").- See Also:
-
DEFAULT_RS
POSIX default record separator ("\n").- See Also:
-
DEFAULT_OFS
POSIX default output field separator (" ").- See Also:
-
DEFAULT_ORS
POSIX default output record separator ("\n").- See Also:
-
DEFAULT_CONVFMT
POSIX default number-to-string conversion format ("%.6g").- See Also:
-
DEFAULT_OFMT
POSIX default output number format ("%.6g").- See Also:
-
DEFAULT_SUBSEP
POSIX default subscript separator ("\034").
-
-
Constructor Details
-
Awk
public Awk()Create a new instance of Awk without extensions. -
Awk
Create a new instance of Awk with the specified settings.- Parameters:
settings- behavioral configuration for this engine
-
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 and settings.- Parameters:
extensions- extension instances implementingJawkExtensionsettings- behavioral configuration for this engine
-
Awk
Create a new instance of Awk with the specified extension instances.- Parameters:
extensions- extension instances implementingJawkExtension
-
-
Method Details
-
getSettings
Returns the behavioral settings associated with this engine instance.- Returns:
- the
AwkSettingsused by this instance, nevernull
-
getLastAst
Returns the last parsed AST produced by the most recent program compilation.- Returns:
- the last
AstNode, ornullif no compilation occurred
-
compile
Compiles a full AWK program.- Parameters:
script- AWK program source- Returns:
- compiled immutable program
- Throws:
IOException- if compilation fails
-
compile
Compiles a full AWK program.- Parameters:
script- AWK program source- Returns:
- compiled immutable program
- Throws:
IOException- if compilation fails
-
createAvm
Creates a reusable runtime backed by oneAVMinstance.- Returns:
- reusable AVM
-
createAvm
Creates a reusable runtime backed by oneAVMinstance, optionally collecting runtime profiling statistics.- Parameters:
profilingEnabled- whether runtime profiling should be enabled- Returns:
- reusable AVM
-
script
Starts building a run request for a compiled AWK program.Use the returned
Awk.AwkRunBuilderto configure input, arguments, variables, and output, then call one of the terminal methods to execute.awk.script(program).input(stream).execute(mySink); String out = awk.script(program).input("hello").execute();- Parameters:
program- compiled program to execute- Returns:
- a builder for configuring and executing the run
-
script
Starts building a run request from an AWK script string.The script is compiled and executed when a terminal method is called. Additional scripts can be appended by calling
Awk.AwkRunBuilder.script(String)on the returned builder.String result = awk.script("{ print toupper($0) }").input("hello").execute();- Parameters:
scriptText- AWK program source- Returns:
- a builder for configuring and executing the run
-
eval
Evaluates a compiled expression using a fresh isolated runtime.- Parameters:
expression- compiled expression- Returns:
- evaluated value
- Throws:
IOException- if evaluation fails
-
eval
Evaluates a compiled expression against one text record using a fresh isolated runtime.- Parameters:
expression- compiled expressioninput- record exposed as$0- Returns:
- evaluated value
- Throws:
IOException- if evaluation fails
-
eval
Evaluates a compiled expression against one structured record source using a fresh isolated runtime.- Parameters:
expression- compiled expressionsource- structured record source- Returns:
- evaluated value
- Throws:
IOException- if evaluation fails
-
compile
Compiles a list of script sources into an immutable AWK program that can be executed by theAVMruntime.- Parameters:
scripts- script sources to compile- Returns:
- compiled immutable program
- Throws:
IOException- if an I/O error occurs while reading the scripts
-
compile
public AwkProgram compile(List<ScriptSource> scripts, boolean disableOptimizeParam) throws IOException Compiles a list of script sources into an immutable AWK program that can be executed by theAVMruntime.- Parameters:
scripts- script sources to compiledisableOptimizeParam-trueto skip tuple optimization- Returns:
- compiled immutable program
- Throws:
IOException- if an I/O error occurs while reading the scripts
-
compileExpression
Compile an expression to evaluate (not a full script).- Parameters:
expression- AWK expression to compile- Returns:
- compiled immutable expression
- Throws:
IOException- if anything goes wrong with the compilation
-
compileExpression
public AwkExpression compileExpression(String expression, boolean disableOptimizeParam) throws IOException Compile an expression to evaluate (not a full script).- Parameters:
expression- AWK expression to compiledisableOptimizeParam-trueto skip tuple optimization- Returns:
- compiled immutable expression
- 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
- Parameters:
expression- Expression to evaluate (e.g.$2 "-" $3)source- structured input source providing the current record- Returns:
- the value of the specified expression
- Throws:
IOException- if anything goes wrong with the evaluation
-
prepareEval
Prepares one text record for repeated expression evaluation and returns the mutableAVMthat will execute those expressions.The returned
AVMis created using the current runtime configuration of thisAwkinstance and binds the provided record once. Later calls toAVM.eval(AwkExpression)reuse the same AVM state without resetting it between expressions, so mutations intentionally leak across evaluations. This is the high-level convenience wrapper around directAVM.prepareForEval(String)andAVM.eval(AwkExpression)usage.- Parameters:
input- non-null text record to expose as$0CallAVM.close()when you are done with the returned interpreter.- Returns:
- prepared AVM ready for repeated
AVM.eval(AwkExpression)calls - Throws:
IOException- if binding the record fails
-
prepareEval
Prepares the first available record from a structuredInputSourcefor repeated expression evaluation and returns the mutableAVMthat will execute those expressions.The returned AVM remains attached to the provided source, so later
getlineoperations and repeatedAVM.prepareForEval(InputSource)calls continue from that source's current position. LaterAVM.eval(AwkExpression)calls reuse the same AVM state without resetting it between expressions, so mutations intentionally leak across evaluations. Close the returned AVM when you are done with it to release any bound input or runtime I/O resources.- Parameters:
source- structured source providing the record to bind- Returns:
- prepared AVM ready for repeated
AVM.eval(AwkExpression)calls - Throws:
IOException- if reading the record fails or the source is exhausted
-
listAvailableExtensions
Lists metadata for theJawkExtensionimplementations discovered on the class path.- Returns:
- list of discovered extension descriptors
-