Skip to content

Commit

Permalink
Merge branch 'develop' into syntax-highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
laurence-flwls authored and lmorg committed Oct 4, 2023
2 parents 4810a75 + 844f56f commit 1d09717
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 100 deletions.
4 changes: 1 addition & 3 deletions builtins/events/onSignalReceived/interrupts_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@

package signaltrap

import "syscall"

var interrupts = map[string]syscall.Signal{}
var interrupts = map[string]nil{}
4 changes: 1 addition & 3 deletions builtins/events/onSignalReceived/interrupts_plan9.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@

package signaltrap

import "syscall"

var interrupts = map[string]syscall.Signal{}
var interrupts = map[string]nil{}
27 changes: 27 additions & 0 deletions lang/expressions/highlight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,30 @@ func TestHlExpression(t *testing.T) {
`$foo=%(bar)`, `$foo = %( bar )`,
})
}

func TestHlArray(t *testing.T) {
testSyntaxHighlighter(t, []string{
`%[]`, `%[ ]`,
`%[true]`, `%[false]`, `%[null]`,
`%[1 2 3]`, `%[1,2,3]`, `%[1, 2, 3]`, `%[ 1 2 3 ]`,
`%[-1 -2 -3]`, `%[-1,-2,-3]`, `%[-1, -2, -3]`, `%[ -1 -2 -3 ]`,
`%[a b c]`, `%[a,b,c]`, `%[a, b, c]`, `%[ a b c ]`,
`%[-a -b -c]`, `%[-a,-b,-c]`, `%[-a, -b, -c]`, `%[ -a -b -c ]`,
`%['a' 'b' 'b']`, `%['a','b','c']`, `%['a', 'b', 'c']`, `%[ 'a' 'b' 'b' ]`,
`%["a" "b" "b"]`, `%["a","b","c"]`, `%["a", "b", "c"]`, `%[ "a" "b" "b" ]`,
})
}

func TestHlArrayNested(t *testing.T) {
testSyntaxHighlighter(t, []string{
`%[1 %[1 2 3] 3]`,
})
}

func TestHlArrayStatement(t *testing.T) {
testSyntaxHighlighter(t, []string{
`echo %[]`, `echo %[ ]`,
`echo %[1 2 3]`,
`echo %[1 %[1 2 3] 3]`,
})
}
111 changes: 54 additions & 57 deletions lang/expressions/node/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,51 @@ import (
)

type ThemeT struct {
Command string
CmdModifier string
Parameter string
Glob string
Number string
Bareword string
Boolean string
Null string
Variable string
Macro string
Escape string
QuotedString string
ArrayItem string
ObjectKey string
ObjectValue string
Operator string
Pipe string
Comment string
Error string
Braces []string

EndCommand string
EndCmdModifier string
EndParameter string
EndGlob string
EndNumber string
EndBareword string
EndBoolean string
EndNull string
EndVariable string
EndMacro string
EndEscape string
EndQuotedString string
EndArrayItem string
EndObjectKey string
EndObjectValue string
EndOperator string
EndPipe string
EndComment string
EndError string
EndBraces []string
Command string
CmdModifier string
Parameter string
Glob string
Number string
Bareword string
Boolean string
Null string
Variable string
Macro string
Escape string
QuotedString string
ArrayItem string
ArraySeparator string
ObjectKey string
ObjectValue string
ObjectSeparator string
Operator string
Pipe string
Comment string
Error string
Braces []string

EndCommand string
EndCmdModifier string
EndParameter string
EndGlob string
EndNumber string
EndBareword string
EndBoolean string
EndNull string
EndVariable string
EndMacro string
EndEscape string
EndQuotedString string
EndArrayItem string
EndArraySeparator string
EndObjectKey string
EndObjectValue string
EndObjectSeparator string
EndOperator string
EndPipe string
EndComment string
EndError string
EndBraces []string

lookup [][]rune
//previousState [][]rune
Expand All @@ -69,7 +73,7 @@ func (theme *ThemeT) CompileTheme() error {
}

theme.lookup = make([][]rune, 100) //H_END_BRACE+len(theme.EndBraces))
theme.bracePair = -1
//theme.bracePair = -1

noColour := !ansi.IsAllowed()

Expand All @@ -86,8 +90,10 @@ func (theme *ThemeT) CompileTheme() error {
theme.lookup[H_ESCAPE] = []rune(ansi.ForceExpandConsts(theme.Escape, noColour))
theme.lookup[H_QUOTED_STRING] = []rune(ansi.ForceExpandConsts(theme.QuotedString, noColour))
theme.lookup[H_ARRAY_ITEM] = []rune(ansi.ForceExpandConsts(theme.ArrayItem, noColour))
theme.lookup[H_ARRAY_SEPARATOR] = []rune(ansi.ForceExpandConsts(theme.ArraySeparator, noColour))
theme.lookup[H_OBJECT_KEY] = []rune(ansi.ForceExpandConsts(theme.ObjectKey, noColour))
theme.lookup[H_OBJECT_VALUE] = []rune(ansi.ForceExpandConsts(theme.ObjectValue, noColour))
theme.lookup[H_OBJECT_SEPARATOR] = []rune(ansi.ForceExpandConsts(theme.ObjectSeparator, noColour))
theme.lookup[H_OPERATOR] = []rune(ansi.ForceExpandConsts(theme.Operator, noColour))
theme.lookup[H_PIPE] = []rune(ansi.ForceExpandConsts(theme.Pipe, noColour))
theme.lookup[H_COMMENT] = []rune(ansi.ForceExpandConsts(theme.Comment, noColour))
Expand All @@ -109,8 +115,10 @@ func (theme *ThemeT) CompileTheme() error {
theme.lookup[H_END_ESCAPE] = []rune(ansi.ForceExpandConsts(_resetStyle(theme.EndEscape), noColour))
theme.lookup[H_END_QUOTED_STRING] = []rune(ansi.ForceExpandConsts(_resetStyle(theme.EndQuotedString), noColour))
theme.lookup[H_END_ARRAY_ITEM] = []rune(ansi.ForceExpandConsts(_resetStyle(theme.EndArrayItem), noColour))
theme.lookup[H_END_ARRAY_SEPARATOR] = []rune(ansi.ForceExpandConsts(_resetStyle(theme.EndArraySeparator), noColour))
theme.lookup[H_END_OBJECT_KEY] = []rune(ansi.ForceExpandConsts(_resetStyle(theme.EndObjectKey), noColour))
theme.lookup[H_END_OBJECT_VALUE] = []rune(ansi.ForceExpandConsts(_resetStyle(theme.EndObjectValue), noColour))
theme.lookup[H_END_OBJECT_SEPARATOR] = []rune(ansi.ForceExpandConsts(_resetStyle(theme.EndObjectSeparator), noColour))
theme.lookup[H_END_OPERATOR] = []rune(ansi.ForceExpandConsts(_resetStyle(theme.EndOperator), noColour))
theme.lookup[H_END_PIPE] = []rune(ansi.ForceExpandConsts(_resetStyle(theme.EndPipe), noColour))
theme.lookup[H_END_COMMENT] = []rune(ansi.ForceExpandConsts(_resetStyle(theme.EndComment), noColour))
Expand All @@ -127,7 +135,7 @@ func (theme *ThemeT) highlight(keyword Symbol, block ...rune) []rune {

r := theme.lookup[v]
r = append(r, block...)
r = append(r, theme.lookup[v+_adjust+1]...)
r = append(r, theme.lookup[v+ADJUST]...)
//r = append(r, theme.previousStyle()...)

return r
Expand All @@ -136,6 +144,8 @@ func (theme *ThemeT) highlight(keyword Symbol, block ...rune) []rune {
func (theme *ThemeT) braceAdj(keyword Symbol) Symbol {
switch keyword {
case H_BRACE_OPEN:
return _H_BRACE
//panic(fmt.Sprintf("%d %d", _H_BRACE+theme.bracePair, _H_BRACE+theme.bracePair+ADJUST))
adj := _H_BRACE + theme.bracePair
theme.bracePair++
if theme.bracePair != 0 && len(theme.lookup[_H_BRACE+theme.bracePair]) == 0 {
Expand All @@ -144,6 +154,7 @@ func (theme *ThemeT) braceAdj(keyword Symbol) Symbol {
return adj

case H_BRACE_CLOSE:
return _H_BRACE
theme.bracePair--
adj := _H_BRACE + theme.bracePair
if adj < 0 {
Expand All @@ -155,17 +166,3 @@ func (theme *ThemeT) braceAdj(keyword Symbol) Symbol {
return keyword
}
}

/*func (theme *ThemeT) addStyle(style []rune) {
theme.previousState = append(theme.previousState, style)
}
func (theme *ThemeT) restoreStyle() []rune {
if len(theme.previousState) == 0 {
return []rune{}
}
style := theme.previousState[len(theme.previousState)-1]
theme.previousState = theme.previousState[:len(theme.previousState)-1]
return style
}
*/
17 changes: 4 additions & 13 deletions lang/expressions/node/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,6 @@ type SyntaxTreeT interface {
SyntaxHighlight() []rune
}

/*Clone() ThemeT
BeginSubExpr()
UpdateParent()
Clear()
ClearExpr()
GetHighlighted() []rune
Begin(int, ...rune)
Append(...rune)
End(int, ...rune)
Highlight(int, ...rune)
IsHighlighter() bool
}*/

type Symbol int

const (
Expand All @@ -39,8 +26,10 @@ const (
H_ESCAPE
H_QUOTED_STRING
H_ARRAY_ITEM
H_ARRAY_SEPARATOR
H_OBJECT_KEY
H_OBJECT_VALUE
H_OBJECT_SEPARATOR
H_OPERATOR
H_PIPE
H_COMMENT
Expand All @@ -61,8 +50,10 @@ const (
H_END_ESCAPE
H_END_QUOTED_STRING
H_END_ARRAY_ITEM
H_END_ARRAY_SEPARATOR
H_END_OBJECT_KEY
H_END_OBJECT_VALUE
H_END_OBJECT_SEPARATOR
H_END_OPERATOR
H_END_PIPE
H_END_COMMENT
Expand Down
31 changes: 30 additions & 1 deletion lang/expressions/parse_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/lmorg/murex/lang"
"github.com/lmorg/murex/lang/expressions/node"
"github.com/lmorg/murex/lang/expressions/primitives"
"github.com/lmorg/murex/lang/expressions/symbols"
"github.com/lmorg/murex/lang/types"
Expand Down Expand Up @@ -61,10 +62,12 @@ func (tree *ParserT) parseArray(exec bool) ([]rune, *primitives.DataType, error)

case '\'', '"':
// quoted string
tree.syntaxTree.Add(node.H_BRACE_OPEN, r)
value, err := tree.parseString(r, r, exec)
if err != nil {
return nil, nil, err
}
tree.syntaxTree.Add(node.H_BRACE_CLOSE, r)
slice = append(slice, string(value))
tree.charPos++

Expand All @@ -74,18 +77,25 @@ func (tree *ParserT) parseArray(exec bool) ([]rune, *primitives.DataType, error)
// do nothing because action covered in the next iteration
case '(':
// start nested string
tree.syntaxTree.Add(node.H_BRACE_OPEN, '%', '(')
tree.charPos++
value, err := tree.parseParenthesis(exec)
if err != nil {
return nil, nil, err
}
slice = append(slice, string(value))
default:
tree.syntaxTree.Add(node.H_ERROR, tree.nextChar())
return nil, nil, fmt.Errorf("'%%' token should be followed with '[', '{', or '(', instead got '%s'", string(r))
}

case '[':
// start nested array
if tree.prevChar() == '%' {
tree.syntaxTree.Add(node.H_BRACE_OPEN, '%', '[')
} else {
tree.syntaxTree.Add(node.H_BRACE_OPEN, '[')
}
_, dt, err := tree.parseArray(exec)
if err != nil {
return nil, nil, err
Expand All @@ -99,10 +109,16 @@ func (tree *ParserT) parseArray(exec bool) ([]rune, *primitives.DataType, error)

case ']':
// end array
tree.syntaxTree.Add(node.H_BRACE_CLOSE, ']')
goto endArray

case '{':
// start nested object
if tree.prevChar() == '%' {
tree.syntaxTree.Add(node.H_BRACE_OPEN, '%', '{')
} else {
tree.syntaxTree.Add(node.H_BRACE_OPEN, '{')
}
_, dt, err := tree.parseObject(exec)
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -193,6 +209,7 @@ func (tree *ParserT) parseArray(exec bool) ([]rune, *primitives.DataType, error)
//fallthrough

case ',', ' ', '\t', '\r':
tree.syntaxTree.Add(node.H_ARRAY_SEPARATOR, r)
// do nothing

default:
Expand All @@ -201,9 +218,21 @@ func (tree *ParserT) parseArray(exec bool) ([]rune, *primitives.DataType, error)
if err == nil {
// is a number
slice = append(slice, v)
tree.syntaxTree.Add(node.H_NUMBER, value...)
} else {
// is a string
slice = append(slice, formatArrayValue(value))
v = formatArrayValue(value)
slice = append(slice, v)
switch v.(type) {
case bool:
tree.syntaxTree.Add(node.H_BOOLEAN, value...)
case nil:
tree.syntaxTree.Add(node.H_NULL, value...)
case string:
tree.syntaxTree.Add(node.H_ARRAY_ITEM, value...)
default:
panic(fmt.Sprintf("unexpected data type: %T", v))
}
}
}
}
Expand Down
Loading

0 comments on commit 1d09717

Please sign in to comment.