Skip to content

Commit

Permalink
Merge pull request #1 from aymanbagabas/refactor
Browse files Browse the repository at this point in the history
refactor,fix: move parser to root and fix bugs
  • Loading branch information
danielgatis authored Sep 15, 2023
2 parents e336dfe + d850c57 commit 2e8421e
Show file tree
Hide file tree
Showing 21 changed files with 5,444 additions and 5,255 deletions.
2 changes: 1 addition & 1 deletion vtparser/_table.go.erb → _table.go.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package vtparser
package vte

// THIS FILE WAS AUTO-GENERATED. DO NOT MODIFY.

Expand Down
268 changes: 268 additions & 0 deletions _tablegen.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
#!/usr/bin/env ruby

require "erb"

action_names = [
:None,
:Clear,
:Collect,
:CsiDispatch,
:EscDispatch,
:Execute,
:Hook,
:Ignore,
:OscEnd,
:OscPut,
:OscStart,
:Param,
:Print,
:Put,
:Unhook,
:BeginUtf8,
]

state_names = [
:Anywhere,
:CsiEntry,
:CsiIgnore,
:CsiIntermediate,
:CsiParam,
:DcsEntry,
:DcsIgnore,
:DcsIntermediate,
:DcsParam,
:DcsPassthrough,
:Escape,
:EscapeIntermediate,
:Ground,
:OscString,
:SosPmApcString,
:Utf8,
]

states = {}

states[:Anywhere] = {
0x18 => [:Ground, :Execute],
0x1a => [:Ground, :Execute],
0x1b => [:Escape, :None],
}

states[:Ground] = {
0x00..0x17 => [:Anywhere, :Execute],
0x19 => [:Anywhere, :Execute],
0x1c..0x1f => [:Anywhere, :Execute],
0x20..0x7f => [:Anywhere, :Print],
0x80..0x8f => [:Anywhere, :Execute],
0x91..0x9a => [:Anywhere, :Execute],
0x9c => [:Anywhere, :Execute],
0xc2..0xdf => [:Utf8, :BeginUtf8], #Beginning of UTF-8 2 byte sequence
0xe0..0xef => [:Utf8, :BeginUtf8], #Beginning of UTF-8 3 byte sequence
0xf0..0xf4 => [:Utf8, :BeginUtf8], #Beginning of UTF-8 4 byte sequence
}

states[:Escape] = {
0x00..0x17 => [:Anywhere, :Execute],
0x19 => [:Anywhere, :Execute],
0x1c..0x1f => [:Anywhere, :Execute],
0x7f => [:Anywhere, :Ignore],
0x20..0x2f => [:EscapeIntermediate, :Collect],
0x30..0x4f => [:Ground, :EscDispatch],
0x51..0x57 => [:Ground, :EscDispatch],
0x59 => [:Ground, :EscDispatch],
0x5a => [:Ground, :EscDispatch],
0x5c => [:Ground, :EscDispatch],
0x60..0x7e => [:Ground, :EscDispatch],
0x5b => [:CsiEntry, :None],
0x5d => [:OscString, :None],
0x50 => [:DcsEntry, :None],
0x58 => [:SosPmApcString, :None],
0x5e => [:SosPmApcString, :None],
0x5f => [:SosPmApcString, :None],
}

states[:EscapeIntermediate] = {
0x00..0x17 => [:Anywhere, :Execute],
0x19 => [:Anywhere, :Execute],
0x1c..0x1f => [:Anywhere, :Execute],
0x20..0x2f => [:Anywhere, :Collect],
0x7f => [:Anywhere, :Ignore],
0x30..0x7e => [:Ground, :EscDispatch],
}

states[:CsiEntry] = {
0x00..0x17 => [:Anywhere, :Execute],
0x19 => [:Anywhere, :Execute],
0x1c..0x1f => [:Anywhere, :Execute],
0x7f => [:Anywhere, :Ignore],
0x20..0x2f => [:CsiIntermediate, :Collect],
0x30..0x39 => [:CsiParam, :Param],
0x3a..0x3b => [:CsiParam, :Param],
0x3c..0x3f => [:CsiParam, :Collect],
0x40..0x7e => [:Ground, :CsiDispatch],
}

states[:CsiIgnore] = {
0x00..0x17 => [:Anywhere, :Execute],
0x19 => [:Anywhere, :Execute],
0x1c..0x1f => [:Anywhere, :Execute],
0x20..0x3f => [:Anywhere, :Ignore],
0x7f => [:Anywhere, :Ignore],
0x40..0x7e => [:Ground, :None],
}

states[:CsiParam] = {
0x00..0x17 => [:Anywhere, :Execute],
0x19 => [:Anywhere, :Execute],
0x1c..0x1f => [:Anywhere, :Execute],
0x30..0x39 => [:Anywhere, :Param],
0x3a..0x3b => [:Anywhere, :Param],
0x7f => [:Anywhere, :Ignore],
0x3c..0x3f => [:CsiIgnore, :None],
0x20..0x2f => [:CsiIntermediate, :Collect],
0x40..0x7e => [:Ground, :CsiDispatch],
}

states[:CsiIntermediate] = {
0x00..0x17 => [:Anywhere, :Execute],
0x19 => [:Anywhere, :Execute],
0x1c..0x1f => [:Anywhere, :Execute],
0x20..0x2f => [:Anywhere, :Collect],
0x7f => [:Anywhere, :Ignore],
0x30..0x3f => [:CsiIgnore, :None],
0x40..0x7e => [:Ground, :CsiDispatch],
}

states[:DcsEntry] = {
0x00..0x17 => [:Anywhere, :Ignore],
0x19 => [:Anywhere, :Ignore],
0x1c..0x1f => [:Anywhere, :Ignore],
0x7f => [:Anywhere, :Ignore],
0x20..0x2f => [:DcsIntermediate, :Collect],
0x30..0x39 => [:DcsParam, :Param],
0x3a..0x3b => [:DcsParam, :Param],
0x3c..0x3f => [:DcsParam, :Collect],
0x40..0x7e => [:DcsPassthrough, :None],
}

states[:DcsIntermediate] = {
0x00..0x17 => [:Anywhere, :Ignore],
0x19 => [:Anywhere, :Ignore],
0x1c..0x1f => [:Anywhere, :Ignore],
0x20..0x2f => [:Anywhere, :Collect],
0x7f => [:Anywhere, :Ignore],
0x30..0x3f => [:DcsIgnore, :None],
0x40..0x7e => [:DcsPassthrough, :None],
}

states[:DcsIgnore] = {
0x00..0x17 => [:Anywhere, :Ignore],
0x19 => [:Anywhere, :Ignore],
0x1c..0x1f => [:Anywhere, :Ignore],
0x20..0x7f => [:Anywhere, :Ignore],
0x9c => [:Ground, :None],
}

states[:DcsParam] = {
0x00..0x17 => [:Anywhere, :Ignore],
0x19 => [:Anywhere, :Ignore],
0x1c..0x1f => [:Anywhere, :Ignore],
0x30..0x39 => [:Anywhere, :Param],
0x3a..0x3b => [:Anywhere, :Param],
0x7f => [:Anywhere, :Ignore],
0x3c..0x3f => [:DcsIgnore, :None],
0x20..0x2f => [:DcsIntermediate, :Collect],
0x40..0x7e => [:DcsPassthrough, :None],
}

states[:DcsPassthrough] = {
0x00..0x17 => [:Anywhere, :Put],
0x19 => [:Anywhere, :Put],
0x1c..0x1f => [:Anywhere, :Put],
0x20..0x7e => [:Anywhere, :Put],
0x7f => [:Anywhere, :Ignore],
0x9c => [:Ground, :None],
}

states[:SosPmApcString] = {
0x00..0x17 => [:Anywhere, :Ignore],
0x19 => [:Anywhere, :Ignore],
0x1c..0x1f => [:Anywhere, :Ignore],
0x20..0x7f => [:Anywhere, :Ignore],
0x9c => [:Ground, :None],
}

states[:OscString] = {
0x00..0x06 => [:Anywhere, :Ignore],
0x07 => [:Ground, :None],
0x08..0x17 => [:Anywhere, :Ignore],
0x19 => [:Anywhere, :Ignore],
0x1c..0x1f => [:Anywhere, :Ignore],
0x20..0xff => [:Anywhere, :OscPut],
}

entry_actions = [
nil,
:Clear,
nil,
nil,
nil,
:Clear,
nil,
nil,
nil,
:Hook,
:Clear,
nil,
nil,
:OscStart,
nil,
nil,
]

exit_actions = [
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
:Unhook,
nil,
nil,
nil,
:OscEnd,
nil,
nil,
]

table = state_names.map do |state|
actions = states[state]
next if actions.nil?

state_actions = Array.new(256, 0)

actions.each do |range, act|
Array(range).each do |r|
state_actions[r] = act
end
end

state_actions
end

table.compact!

if __FILE__ == $0
template_path = File.read(File.expand_path("_table.go.erb", File.dirname(__FILE__)))
out_path = File.expand_path("table.go", File.dirname(__FILE__))

renderer = ERB.new(template_path, nil, "-")
output = renderer.result(binding)

File.write(out_path, output)
`go fmt #{out_path}`
end
24 changes: 6 additions & 18 deletions main.go → examples/parserlog/main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package main

//go:generate ruby ./utf8/_tablegen.rb
//go:generate ruby ./vtparser/_tablegen.rb

import (
"bufio"
"fmt"
"io"
"os"

"github.com/danielgatis/go-vte/vtparser"
"github.com/danielgatis/go-vte"
)

type dispatcher struct{}
Expand All @@ -30,16 +27,16 @@ func (p *dispatcher) Unhook() {
fmt.Printf("[Unhook]\n")
}

func (p *dispatcher) Hook(params []int64, intermediates []byte, ignore bool, r rune) {
fmt.Printf("[Hook] params=%v, intermediates=%v, ignore=%v, r=%v\n", params, intermediates, ignore, r)
func (p *dispatcher) Hook(params [][]uint16, intermediates []byte, ignore bool, r rune) {
fmt.Printf("[Hook] params=%v, intermediates=%v, ignore=%v, r=%c\n", params, intermediates, ignore, r)
}

func (p *dispatcher) OscDispatch(params [][]byte, bellTerminated bool) {
fmt.Printf("[OscDispatch] params=%v, bellTerminated=%v\n", params, bellTerminated)
}

func (p *dispatcher) CsiDispatch(params []int64, intermediates []byte, ignore bool, r rune) {
fmt.Printf("[CsiDispatch] params=%v, intermediates=%v, ignore=%v, r=%v\n", params, intermediates, ignore, r)
func (p *dispatcher) CsiDispatch(params [][]uint16, intermediates []byte, ignore bool, r rune) {
fmt.Printf("[CsiDispatch] params=%v, intermediates=%v, ignore=%v, r=%c\n", params, intermediates, ignore, r)
}

func (p *dispatcher) EscDispatch(intermediates []byte, ignore bool, b byte) {
Expand All @@ -49,16 +46,7 @@ func (p *dispatcher) EscDispatch(intermediates []byte, ignore bool, b byte) {
func main() {
reader := bufio.NewReader(os.Stdin)
dispatcher := &dispatcher{}
parser := vtparser.New(
dispatcher.Print,
dispatcher.Execute,
dispatcher.Put,
dispatcher.Unhook,
dispatcher.Hook,
dispatcher.OscDispatch,
dispatcher.CsiDispatch,
dispatcher.EscDispatch,
)
parser := vte.NewParser(dispatcher)

buff := make([]byte, 2048)

Expand Down
4 changes: 4 additions & 0 deletions gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package vte

//go:generate ruby ./utf8/_tablegen.rb
//go:generate ruby ./_tablegen.rb
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
module github.com/danielgatis/go-vte

go 1.14
go 1.20

require github.com/stretchr/testify v1.5.1

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v2 v2.2.2 // indirect
)
Loading

0 comments on commit 2e8421e

Please sign in to comment.