implement basic VT parser -- only print/execute handled

This commit is contained in:
Mitchell Hashimoto
2022-04-18 11:01:47 -07:00
parent 94f3c5e490
commit 20f9ad3f55
2 changed files with 62 additions and 27 deletions

View File

@ -27,7 +27,10 @@ pub const State = enum {
sos_pm_apc_string, sos_pm_apc_string,
}; };
pub const Action = enum { /// Transition action is an action that can be taken during a state
/// transition. This is more of an internal action, not one used by
/// end users, typically.
pub const TransitionAction = enum {
none, none,
ignore, ignore,
print, print,
@ -45,6 +48,16 @@ pub const Action = enum {
osc_end, osc_end,
}; };
/// Action is the action that a caller of the parser is expected to
/// take as a result of some input character.
pub const Action = union(enum) {
/// Draw character to the screen.
print: u8,
/// Execute the C0 or C1 function.
execute: u8,
};
/// Current state of the state machine /// Current state of the state machine
state: State = .ground, state: State = .ground,
@ -52,7 +65,10 @@ pub fn init() Parser {
return .{}; return .{};
} }
pub fn next(self: *Parser, c: u8) void { /// Next consums the next character c and returns the actions to execute.
/// Up to 3 actions may need to be exected -- in order -- representing
/// the state exit, transition, and entry actions.
pub fn next(self: *Parser, c: u8) [3]?Action {
const effect = effect: { const effect = effect: {
// First look up the transition in the anywhere table. // First look up the transition in the anywhere table.
const anywhere = table[c][@enumToInt(State.anywhere)]; const anywhere = table[c][@enumToInt(State.anywhere)];
@ -65,43 +81,62 @@ pub fn next(self: *Parser, c: u8) void {
const next_state = effect.state; const next_state = effect.state;
const action = effect.action; const action = effect.action;
// After generating the actions, we set our next state.
defer self.state = next_state;
// When going from one state to another, the actions take place in this order: // When going from one state to another, the actions take place in this order:
// //
// 1. exit action from old state // 1. exit action from old state
// 2. transition action // 2. transition action
// 3. entry action to new state // 3. entry action to new state
return [3]?Action{
// Perform exit actions. "The action associated with the exit event happens
// when an incoming symbol causes a transition from this state to another
// state (or even back to the same state)."
switch (self.state) { switch (self.state) {
.osc_string => {}, // TODO: osc_end .osc_string => @panic("TODO"), // TODO: osc_end
.dcs_passthrough => {}, // TODO: unhook .dcs_passthrough => @panic("TODO"), // TODO: unhook
else => {}, else => null,
} },
// Perform the transition action self.doAction(action, c),
self.doAction(action);
// Perform the entry action
// TODO: when _first_ entered only?
switch (self.state) { switch (self.state) {
.escape, .dcs_entry, .csi_entry => {}, // TODO: clear .escape, .dcs_entry, .csi_entry => @panic("TODO"), // TODO: clear
.osc_string => {}, // TODO: osc_start .osc_string => @panic("TODO"), // TODO: osc_start
.dcs_passthrough => {}, // TODO: hook .dcs_passthrough => @panic("TODO"), // TODO: hook
else => {}, else => null,
} },
};
self.state = next_state;
} }
fn doAction(self: *Parser, action: Action) void { fn doAction(self: *Parser, action: TransitionAction, c: u8) ?Action {
_ = self; _ = self;
_ = action; return switch (action) {
.none, .ignore => null,
.print => return Action{ .print = c },
.execute => return Action{ .execute = c },
else => @panic("TODO"),
};
} }
test { test {
var p = init(); var p = init();
p.next(0x9E); _ = p.next(0x9E);
try testing.expect(p.state == .sos_pm_apc_string); try testing.expect(p.state == .sos_pm_apc_string);
_ = p.next(0x9C);
try testing.expect(p.state == .ground);
{
const a = p.next('a');
try testing.expect(p.state == .ground);
try testing.expect(a[0] == null);
try testing.expect(a[1].? == .print);
try testing.expect(a[2] == null);
}
{
const a = p.next(0x19);
try testing.expect(p.state == .ground);
try testing.expect(a[0] == null);
try testing.expect(a[1].? == .execute);
try testing.expect(a[2] == null);
}
} }

View File

@ -5,7 +5,7 @@ const std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
const parser = @import("Parser.zig"); const parser = @import("Parser.zig");
const State = parser.State; const State = parser.State;
const Action = parser.Action; const Action = parser.TransitionAction;
/// The state transition table. The type is [u8][State]Transition but /// The state transition table. The type is [u8][State]Transition but
/// comptime-generated to be exactly-sized. /// comptime-generated to be exactly-sized.