diff options
| author | Marc Vertes <mvertes@free.fr> | 2023-11-10 22:08:46 +0100 |
|---|---|---|
| committer | Marc Vertes <mvertes@free.fr> | 2023-11-10 22:08:46 +0100 |
| commit | bec71a19e7d7cd0847d1cfa6ef2110d7301fcdd1 (patch) | |
| tree | 7d977e9ad86c119603db88d3fdbf689a845ff8e5 /parser/tokens.go | |
| parent | 5220ccb741c7f3688731d3b3df6e5e851f50f5c5 (diff) | |
parser: implement support for var declarations
The full Go syntax is supported, blocks or line,
mutiple comma separated variables, assignments.
In local and global frame.
Diffstat (limited to 'parser/tokens.go')
| -rw-r--r-- | parser/tokens.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/parser/tokens.go b/parser/tokens.go new file mode 100644 index 0000000..acffe58 --- /dev/null +++ b/parser/tokens.go @@ -0,0 +1,57 @@ +package parser + +import ( + "fmt" + + "github.com/gnolang/parscan/lang" + "github.com/gnolang/parscan/scanner" +) + +type Tokens []scanner.Token + +func (toks Tokens) String() (s string) { + for _, t := range toks { + s += fmt.Sprintf("%#v ", t.Str) + } + return s +} + +func (toks Tokens) Index(id lang.TokenId) int { + for i, t := range toks { + if t.Id == id { + return i + } + } + return -1 +} + +func (toks Tokens) LastIndex(id lang.TokenId) int { + for i := len(toks) - 1; i >= 0; i-- { + if toks[i].Id == id { + return i + } + } + return -1 +} + +func (toks Tokens) Split(id lang.TokenId) (result []Tokens) { + for { + i := toks.Index(id) + if i < 0 { + return append(result, toks) + } + result = append(result, toks[:i]) + toks = toks[i+1:] + } +} + +func (toks Tokens) SplitStart(id lang.TokenId) (result []Tokens) { + for { + i := toks[1:].Index(id) + if i < 0 { + return append(result, toks) + } + result = append(result, toks[:i]) + toks = toks[i+1:] + } +} |
