awk

    AWK 记录处理器本地候选

    Download zip
    Version
    0.10.2
    License
    MIT AND BSD-3-Clause
    Last updated
    last month
    Downloads
    30

    No dependencies

    #可执行 API 示例

    单字符分隔符保留空字段,空分隔符按 Unicode 字符拆分。这些例子调用公开 API,并随 moon test 执行。

    ///|
    test "literal separator retains empty columns" {
    assert_eq(@awk.run("{ print NF, $2, $3 }", "a;;7", separator=";"), "3 7\n")
    assert_eq(@awk.run("{print NF,$2}", "abc", separator=""), "3 b\n")
    }

    0.7 增加 CSV/TSV、表头和命名字段,可向 run/run_with_status/new_session 传 input_mode、output_mode。保留文件/管道与按需命令 Worker;完整 Unicode、字节模式、固定参考 BOM 差异及全部上游兼容性仍未完成。

    0.8 增加 Unicode 正则属性、完整简单大小写等价映射、十六进制与引号转义及命名分组;具体参考版本边界见 README。

    0.9 的 tolower/toupper 使用固定 Unicode 15.0.0 简单映射,支持非 ASCII 字符;所有有效码点的原版结果均进入 JS/Wasm-GC 回归。

    0.10 修复正则重复、引号与行内标志边界,保留 FS 和 split 对空匹配的不同处理。公开 API 未改变,详细语义和独立原版验证范围见 README 与 TESTING。

    ParseError

    pub suberror ParseError {
    Invalid(String)
    InputFailure(String)
    } derive(
    Debug
    )

    IOReply

    pub(all) enum IOReply {
    IOReady
    IOData(String)
    IOEnd
    IOStatus(Int)
    IOFailed(String)
    }

    IORequest

    pub(all) enum IORequest {
    OpenReader(String, Bool)
    ReadReader(String)
    OpenWriter(String, String)
    WriteWriter(String, String)
    CloseStream(String)
    FlushStream(String)
    Execute(String)
    }

    Host operations for explicit AWK redirection. The core owns stream names, reuse and record scanning; the host owns descriptors and child processes.

    InputReply

    pub(all) enum InputReply {
    Ready
    Chunk(String)
    End
    Failed(String)
    }

    InputRequest

    pub(all) enum InputRequest {
    Open(String)
    Read
    Close
    }

    Pull input operations. A host opens one file at a time and supplies UTF-8 decoded chunks (at most 65,536 UTF-16 units), without splitting records.

    RunResult

    pub(all) struct RunResult {
    output : String
    exit_status : Int
    steps : Int
    } derive(Eq, ToJson,
    Debug
    )

    RunResult::equal

    fn RunResult::equal(RunResult, RunResult) -> Bool

    RunResult::not_equal

    fn RunResult::not_equal(x : RunResult, y : RunResult) -> Bool

    RunResult::to_json

    fn RunResult::to_json(RunResult) -> Json

    Session

    pub struct Session {
    // private fields
    }

    A compiled program and its mutable execution state; instances are independent.

    Session::advance

    fn Session::advance(self : Session) -> Bool raise ParseError

    Process the next record from the configured pull source. getline shares that source, including reads in BEGIN, functions, and END.

    Session::argument

    fn Session::argument(self : Session, index : Int) -> String raise ParseError

    Session::argument_count

    fn Session::argument_count(self : Session) -> Int raise ParseError

    Session::assign

    fn Session::assign(self : Session, name : String, text : String) -> Unit raise ParseError

    Apply a command-line assignment with AWK escape and numeric-string semantics.

    Session::begin

    fn Session::begin(self : Session) -> Unit raise ParseError

    Session::begin_file

    fn Session::begin_file(self : Session, name : String) -> Unit raise ParseError

    Session::close_input

    fn Session::close_input(self : Session) -> Unit

    Release a pull source, also on failure or early exit; END must run first.

    Session::close_io

    fn Session::close_io(self : Session) -> Unit

    Close all redirected streams after END or after an execution error. Like GoAWK's implicit cleanup, close failures do not replace the exit status.

    Session::drain

    fn Session::drain(self : Session) -> String

    Session::exit_status

    fn Session::exit_status(self : Session) -> Int

    Session::feed

    fn Session::feed(self : Session, record : String) -> Unit raise ParseError

    Process one record, without its separator. Does not retain earlier records.

    Session::finish

    fn Session::finish(self : Session) -> Unit raise ParseError

    Session::is_stopped

    fn Session::is_stopped(self : Session) -> Bool

    Session::needs_input

    fn Session::needs_input(self : Session) -> Bool

    Whether record or END rules require the host to consume input.

    Session::should_skip_file

    fn Session::should_skip_file(self : Session) -> Bool

    Session::steps

    fn Session::steps(self : Session) -> Int

    new_session

    fn new_session(program : String, separator? : String, input_mode? : String, output_mode? : String, variables? : Map[String, String], environment? : Map[String, String], arguments? : Array[String], step_limit? : Int, clock_seconds? : Double, io? : (IORequest) -> IOReply?, input? : (InputRequest) -> InputReply?, output_sink? : (String) -> String??) -> Session raise ParseError

    run

    fn run(program : String, input : String, separator? : String, input_mode? : String, output_mode? : String) -> String raise ParseError

    run_with_status

    fn run_with_status(program : String, input : String, separator? : String, input_mode? : String, output_mode? : String, step_limit? : Int, clock_seconds? : Double) -> RunResult raise ParseError

    Run an in-memory stream. For incremental file processing use Session.