MCPcopy Create free account
hub / github.com/dengsgo/math-engine / parsePrimary

Method parsePrimary

engine/ast.go:182–235  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

180}
181
182func (a *AST) parsePrimary() ExprAST {
183 switch a.currTok.Type {
184 case Identifier:
185 return a.parseFunCallerOrConst()
186 case Literal:
187 return a.parseNumber()
188 case Operator:
189 if a.currTok.Tok == "(" {
190 t := a.getNextToken()
191 if t == nil {
192 a.Err = errors.New(
193 fmt.Sprintf("want '(' or '0-9' but get EOF\n%s",
194 ErrPos(a.source, a.currTok.Offset)))
195 return nil
196 }
197 e := a.ParseExpression()
198 if e == nil {
199 return nil
200 }
201 if a.currTok.Tok != ")" {
202 a.Err = errors.New(
203 fmt.Sprintf("want ')' but get %s\n%s",
204 a.currTok.Tok,
205 ErrPos(a.source, a.currTok.Offset)))
206 return nil
207 }
208 a.getNextToken()
209 return e
210 } else if a.currTok.Tok == "-" {
211 if a.getNextToken() == nil {
212 a.Err = errors.New(
213 fmt.Sprintf("want '0-9' but get '-'\n%s",
214 ErrPos(a.source, a.currTok.Offset)))
215 return nil
216 }
217 bin := BinaryExprAST{
218 Op: "-",
219 Lhs: NumberExprAST{},
220 Rhs: a.parsePrimary(),
221 }
222 return bin
223 } else {
224 return a.parseNumber()
225 }
226 case COMMA:
227 a.Err = errors.New(
228 fmt.Sprintf("want '(' or '0-9' but get %s\n%s",
229 a.currTok.Tok,
230 ErrPos(a.source, a.currTok.Offset)))
231 return nil
232 default:
233 return nil
234 }
235}
236
237func (a *AST) parseBinOpRHS(execPrec int, lhs ExprAST) ExprAST {
238 for {

Callers 2

ParseExpressionMethod · 0.95
parseBinOpRHSMethod · 0.95

Calls 5

parseFunCallerOrConstMethod · 0.95
parseNumberMethod · 0.95
getNextTokenMethod · 0.95
ParseExpressionMethod · 0.95
ErrPosFunction · 0.85

Tested by

no test coverage detected