MCPcopy Create free account
hub / github.com/cogentcore/core / TranspileLineTokens

Method TranspileLineTokens

shell/transpile.go:41–140  ·  view source on GitHub ↗

TranspileLineTokens returns the tokens for the full line

(ln string)

Source from the content-addressed store, hash-verified

39
40// TranspileLineTokens returns the tokens for the full line
41func (sh *Shell) TranspileLineTokens(ln string) Tokens {
42 if ln == "" {
43 return nil
44 }
45 toks := sh.Tokens(ln)
46 n := len(toks)
47 if n == 0 {
48 return toks
49 }
50 ewords, err := ExecWords(ln)
51 if err != nil {
52 sh.AddError(err)
53 return nil
54 }
55 logx.PrintlnDebug("\n########## line:\n", ln, "\nTokens:\n", toks.String(), "\nWords:\n", ewords)
56
57 if toks[0].Tok == token.TYPE {
58 sh.TypeDepth++
59 }
60 if toks[0].Tok == token.IMPORT || toks[0].Tok == token.VAR || toks[0].Tok == token.CONST {
61 sh.DeclDepth++
62 }
63
64 if sh.TypeDepth > 0 || sh.DeclDepth > 0 {
65 logx.PrintlnDebug("go: type / decl defn")
66 return sh.TranspileGo(toks)
67 }
68
69 t0 := toks[0]
70 _, t0pn := toks.Path(true) // true = first position
71 en := len(ewords)
72
73 f0exec := (t0.Tok == token.IDENT && ExecWordIsCommand(ewords[0]))
74
75 switch {
76 case t0.Tok == token.LBRACE:
77 logx.PrintlnDebug("go: { } line")
78 return sh.TranspileGo(toks[1 : n-1])
79 case t0.Tok == token.LBRACK:
80 logx.PrintlnDebug("exec: [ ] line")
81 return sh.TranspileExec(ewords, false) // it processes the [ ]
82 case t0.Tok == token.ILLEGAL:
83 logx.PrintlnDebug("exec: illegal")
84 return sh.TranspileExec(ewords, false)
85 case t0.IsBacktickString():
86 logx.PrintlnDebug("exec: backquoted string")
87 exe := sh.TranspileExecString(t0.Str, false)
88 if n > 1 { // todo: is this an error?
89 exe.AddTokens(sh.TranspileGo(toks[1:]))
90 }
91 return exe
92 case t0.Tok == token.IDENT && t0.Str == "command":
93 sh.lastCommand = toks[1].Str // 1 is the name -- triggers AddCommand
94 toks = toks[2:] // get rid of first
95 toks.Insert(0, token.IDENT, "shell.AddCommand")
96 toks.Insert(1, token.LPAREN)
97 toks.Insert(2, token.STRING, `"`+sh.lastCommand+`"`)
98 toks.Insert(3, token.COMMA)

Callers 1

TranspileLineMethod · 0.95

Calls 15

TokensMethod · 0.95
AddErrorMethod · 0.95
TranspileGoMethod · 0.95
TranspileExecMethod · 0.95
TranspileExecStringMethod · 0.95
PrintlnDebugFunction · 0.92
ExecWordsFunction · 0.85
ExecWordIsCommandFunction · 0.85
IsBacktickStringMethod · 0.80
AddTokensMethod · 0.80
IsGoMethod · 0.80
StringMethod · 0.65

Tested by

no test coverage detected