Load syntax rules from file
This commit is contained in:
parent
068260157c
commit
6a27593800
3 changed files with 38 additions and 28 deletions
|
@ -1,40 +1,35 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gdamore/tcell"
|
||||
"io/ioutil"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var syntaxRules string
|
||||
|
||||
func GetRules() string {
|
||||
file, err := ioutil.ReadFile("syntax.micro")
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(file)
|
||||
}
|
||||
|
||||
// Match ...
|
||||
func Match(str string) map[int]tcell.Style {
|
||||
rules := `color blue "[A-Za-z_][A-Za-z0-9_]*[[:space:]]*[()]"
|
||||
color blue "\b(append|cap|close|complex|copy|delete|imag|len)\b"
|
||||
color blue "\b(make|new|panic|print|println|protect|real|recover)\b"
|
||||
color green "\b(u?int(8|16|32|64)?|float(32|64)|complex(64|128))\b"
|
||||
color green "\b(uintptr|byte|rune|string|interface|bool|map|chan|error)\b"
|
||||
color cyan "\b(package|import|const|var|type|struct|func|go|defer|nil|iota)\b"
|
||||
color cyan "\b(for|range|if|else|case|default|switch|return)\b"
|
||||
color red "\b(go|goto|break|continue)\b"
|
||||
color cyan "\b(true|false)\b"
|
||||
color red "[-+/*=<>!~%&|^]|:="
|
||||
color blue "\b([0-9]+|0x[0-9a-fA-F]*)\b|'.'"
|
||||
color magenta "\\([0-7]{3}|x[A-Fa-f0-9]{2}|u[A-Fa-f0-9]{4}|U[A-Fa-f0-9]{8})"
|
||||
color yellow "` + "`" + `[^` + "`" + `]*` + "`" + `"
|
||||
color green "(^|[[:space:]])//.*"
|
||||
color brightwhite,cyan "TODO:?"
|
||||
color ,green "[[:space:]]+$"
|
||||
color ,red " + +| + +"`
|
||||
rules := strings.TrimSpace(GetRules())
|
||||
|
||||
lines := strings.Split(rules, "\n")
|
||||
m := make(map[int]tcell.Style)
|
||||
parser, _ := regexp.Compile(`color (.*?)\s+"(.*)"`)
|
||||
for _, line := range lines {
|
||||
split := strings.Split(line, "\"")
|
||||
color := strings.Split(split[0], " ")[1]
|
||||
regex, err := regexp.Compile(split[1])
|
||||
submatch := parser.FindSubmatch([]byte(line))
|
||||
color := string(submatch[1])
|
||||
regex, err := regexp.Compile(string(submatch[2]))
|
||||
if err != nil {
|
||||
fmt.Println("\a")
|
||||
continue
|
||||
// Error with the regex!
|
||||
}
|
||||
st := StringToStyle(color)
|
||||
|
@ -48,7 +43,9 @@ color ,red " + +| + +"`
|
|||
}
|
||||
}
|
||||
m[value[0]] = st
|
||||
m[value[1]] = tcell.StyleDefault
|
||||
if _, exists := m[value[1]]; !exists {
|
||||
m[value[1]] = tcell.StyleDefault
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
13
micro.go
13
micro.go
|
@ -2,11 +2,11 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gdamore/tcell"
|
||||
"github.com/go-errors/errors"
|
||||
"github.com/mattn/go-isatty"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/gdamore/tcell"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -47,6 +47,15 @@ func main() {
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
s.Fini()
|
||||
fmt.Println("Micro encountered an error:", err)
|
||||
fmt.Print(errors.Wrap(err, 2).ErrorStack())
|
||||
os.Exit(1)
|
||||
}
|
||||
}()
|
||||
|
||||
defStyle := tcell.StyleDefault.
|
||||
Background(tcell.ColorDefault).
|
||||
Foreground(tcell.ColorDefault)
|
||||
|
|
10
view.go
10
view.go
|
@ -309,7 +309,7 @@ func (v *View) Display() {
|
|||
// + 1 for the little space after the line number
|
||||
v.lineNumOffset = maxLineLength + 1
|
||||
|
||||
var lineStyle tcell.Style
|
||||
var highlightStyle tcell.Style
|
||||
|
||||
for lineN := 0; lineN < v.height; lineN++ {
|
||||
if lineN+v.topline >= len(v.buf.lines) {
|
||||
|
@ -337,15 +337,19 @@ func (v *View) Display() {
|
|||
// Write the line
|
||||
tabchars := 0
|
||||
for _, ch := range line {
|
||||
var lineStyle tcell.Style
|
||||
st, ok := matches[charNum]
|
||||
if ok {
|
||||
lineStyle = st
|
||||
highlightStyle = st
|
||||
}
|
||||
|
||||
if v.cursor.HasSelection() &&
|
||||
(charNum >= v.cursor.selectionStart && charNum <= v.cursor.selectionEnd ||
|
||||
charNum <= v.cursor.selectionStart && charNum >= v.cursor.selectionEnd) {
|
||||
lineStyle = tcell.StyleDefault
|
||||
lineStyle = lineStyle.Reverse(true)
|
||||
} else {
|
||||
lineStyle = highlightStyle
|
||||
}
|
||||
|
||||
if ch == '\t' {
|
||||
|
@ -363,7 +367,7 @@ func (v *View) Display() {
|
|||
x = 0
|
||||
st, ok := matches[charNum]
|
||||
if ok {
|
||||
lineStyle = st
|
||||
highlightStyle = st
|
||||
}
|
||||
charNum++
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue