---------------------------------------------------------------------------- Error handling syntaxErrorAt reports a syntax error at the given position.
(pos Pos, msg string)
| 153 | |
| 154 | // syntaxErrorAt reports a syntax error at the given position. |
| 155 | func (p *parser) syntaxErrorAt(pos Pos, msg string) { |
| 156 | if trace { |
| 157 | p.print("syntax error: " + msg) |
| 158 | } |
| 159 | |
| 160 | if p.tok == _EOF && p.first != nil { |
| 161 | return // avoid meaningless follow-up errors |
| 162 | } |
| 163 | |
| 164 | // add punctuation etc. as needed to msg |
| 165 | switch { |
| 166 | case msg == "": |
| 167 | // nothing to do |
| 168 | case strings.HasPrefix(msg, "in "), strings.HasPrefix(msg, "at "), strings.HasPrefix(msg, "after "): |
| 169 | msg = " " + msg |
| 170 | case strings.HasPrefix(msg, "expecting "): |
| 171 | msg = ", " + msg |
| 172 | default: |
| 173 | // plain error - we don't care about current token |
| 174 | p.errorAt(pos, "syntax error: "+msg) |
| 175 | return |
| 176 | } |
| 177 | |
| 178 | // determine token string |
| 179 | var tok string |
| 180 | switch p.tok { |
| 181 | case _Name, _Semi: |
| 182 | tok = p.lit |
| 183 | case _Literal: |
| 184 | tok = "literal " + p.lit |
| 185 | case _Operator: |
| 186 | tok = p.op.String() |
| 187 | case _AssignOp: |
| 188 | tok = p.op.String() + "=" |
| 189 | case _IncOp: |
| 190 | tok = p.op.String() |
| 191 | tok += tok |
| 192 | default: |
| 193 | tok = tokstring(p.tok) |
| 194 | } |
| 195 | |
| 196 | p.errorAt(pos, "syntax error: unexpected "+tok+msg) |
| 197 | } |
| 198 | |
| 199 | // tokstring returns the English word for selected punctuation tokens |
| 200 | // for more readable error messages. Use tokstring (not tok.String()) |
no test coverage detected