added mod_tag

master
Paulo Simão 2021-11-02 21:38:03 -03:00
parent 4f6c373934
commit 2665d10c09
2 changed files with 42 additions and 7 deletions

41
lib.go
View File

@ -24,6 +24,10 @@ func (m Mode) String() string {
return "ESCAPE"
case MODE_ESCAPE_STR:
return "ESCAPE_STR"
case MODE_TAG:
return "MODE_TAG"
case MODE_ESCAPE_TAG:
return "MODE_ESCAPE_TAG"
}
return "UNKNOWN"
@ -32,8 +36,10 @@ func (m Mode) String() string {
const (
MODE_NORMAL Mode = iota
MODE_STR
MODE_TAG
MODE_ESCAPE
MODE_ESCAPE_STR
MODE_ESCAPE_TAG
)
type LineType int
@ -77,7 +83,7 @@ func Tokens(str string) ([]string, error) {
params = append(params, sb.String())
}
sb = strings.Builder{}
case MODE_STR:
case MODE_STR, MODE_TAG:
sb.WriteRune(a)
default:
return nil, errors.New(fmt.Sprintf("Error at char %d - cant add space here. Mode: %s", i, mode.String()))
@ -99,10 +105,23 @@ func Tokens(str string) ([]string, error) {
switch mode {
case MODE_NORMAL:
if sb.Len() > 0 {
lchar := sb.String()[sb.Len()-1]
if lchar == '=' {
sb.WriteRune(a)
mode = MODE_TAG
continue
}
params = append(params, sb.String())
sb = strings.Builder{}
mode = MODE_STR
}
case MODE_TAG:
sb.WriteRune(a)
params = append(params, sb.String())
sb = strings.Builder{}
mode = MODE_STR
mode = MODE_NORMAL
case MODE_STR:
params = append(params, sb.String())
sb = strings.Builder{}
@ -114,12 +133,15 @@ func Tokens(str string) ([]string, error) {
sb.WriteRune(a)
mode = MODE_STR
}
case '\\':
switch mode {
case MODE_NORMAL:
mode = MODE_ESCAPE
case MODE_STR:
mode = MODE_ESCAPE_STR
case MODE_TAG:
mode = MODE_ESCAPE_TAG
case MODE_ESCAPE:
sb.WriteString("\\")
mode = MODE_NORMAL
@ -129,16 +151,18 @@ func Tokens(str string) ([]string, error) {
}
default:
switch mode {
case MODE_NORMAL:
sb.WriteRune(a)
case MODE_STR:
case MODE_NORMAL, MODE_TAG, MODE_STR:
sb.WriteRune(a)
case MODE_ESCAPE:
sb.WriteString("\\" + string(a))
mode = MODE_NORMAL
case MODE_ESCAPE_STR:
sb.WriteString("\\" + string(a))
mode = MODE_STR
case MODE_ESCAPE_TAG:
sb.WriteString("\\" + string(a))
mode = MODE_TAG
}
}
@ -185,6 +209,7 @@ func Lines(str string) ([]Line, error) {
for _, tk := range tokens {
switch tk {
case "\n", ";", "&&":
if len(line.Tokens) > 0 {
addLine(line)
@ -201,7 +226,13 @@ func Lines(str string) ([]Line, error) {
Tokens: make([]string, 0),
}
default:
//if len(line.Tokens) > 2 && line.Tokens[len(line.Tokens)-1] == "=" {
// line.Tokens[len(line.Tokens)-2] = line.Tokens[len(line.Tokens)-2] + "=" + tk
// line.Tokens = line.Tokens[:len(line.Tokens)-1]
//} else {
line.Tokens = append(line.Tokens, tk)
//}
}
}
if len(line.Tokens) > 0 {

View File

@ -8,9 +8,13 @@ import (
)
func TestLines(t *testing.T) {
lines, err := Lines("; \";\" ;;")
//lines, err := Lines("; \";\" ;;")
//Lines("a\nb\\\\nc")
// Lines(`
lines, err := Lines(`
go build -o main.exe -ldflags="a=1 b=2" ./main.go
`)
//lines, err := Lines(`
//go build -o main.exe -ldflags="a=1 b=2" ./main.go
//"A STR" 1 2 "a b" 1.23 \n &
//"STR \\ with escape \n chars \t" 1 234 true
//ls -larth