mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-07-10 07:05:54 +00:00
Adapt the builtin parsing and resolving to also support operators. Will be used to generate intrinsic table entries for operators. This will simplify maintenance of the operators, and will greatly simplify the [AbstractInt -> i32|u32] [AbstractFloat -> f32|f16] logic. Bug: tint:1504 Change-Id: Id75735ea24e501877418812185796f3fba88a521 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/89026 Commit-Queue: Ben Clayton <bclayton@chromium.org> Kokoro: Kokoro <noreply+kokoro@google.com> Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
137 lines
3.3 KiB
Go
137 lines
3.3 KiB
Go
// Copyright 2021 The Tint Authors.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
// Package tok defines tokens that are produced by the Tint intrinsic definition
|
|
// lexer
|
|
package tok
|
|
|
|
import "fmt"
|
|
|
|
// Kind is an enumerator of token kinds
|
|
type Kind string
|
|
|
|
// Token enumerator types
|
|
const (
|
|
InvalidToken Kind = "<invalid>"
|
|
Identifier Kind = "ident"
|
|
Integer Kind = "integer"
|
|
String Kind = "string"
|
|
Match Kind = "match"
|
|
Function Kind = "fn"
|
|
Operator Kind = "op"
|
|
Type Kind = "type"
|
|
Enum Kind = "enum"
|
|
Colon Kind = ":"
|
|
Comma Kind = ","
|
|
Shl Kind = "<<"
|
|
Shr Kind = ">>"
|
|
Lt Kind = "<"
|
|
Le Kind = "<="
|
|
Gt Kind = ">"
|
|
Ge Kind = ">="
|
|
Lbrace Kind = "{"
|
|
Rbrace Kind = "}"
|
|
Ldeco Kind = "[["
|
|
Rdeco Kind = "]]"
|
|
Lparen Kind = "("
|
|
Rparen Kind = ")"
|
|
Or Kind = "|"
|
|
Arrow Kind = "->"
|
|
Star Kind = "*"
|
|
Divide Kind = "/"
|
|
Modulo Kind = "%"
|
|
Xor Kind = "^"
|
|
Plus Kind = "+"
|
|
Minus Kind = "-"
|
|
And Kind = "&"
|
|
AndAnd Kind = "&&"
|
|
OrOr Kind = "||"
|
|
NotEqual Kind = "!="
|
|
Equal Kind = "=="
|
|
Assign Kind = "="
|
|
)
|
|
|
|
// Invalid represents an invalid token
|
|
var Invalid = Token{Kind: InvalidToken}
|
|
|
|
// Location describes a rune location in the source code
|
|
type Location struct {
|
|
// 1-based line index
|
|
Line int
|
|
// 1-based column index
|
|
Column int
|
|
// 0-based rune index
|
|
Rune int
|
|
// Optional file path
|
|
Filepath string
|
|
}
|
|
|
|
// Format implements the fmt.Formatter interface
|
|
func (l Location) Format(w fmt.State, verb rune) {
|
|
if w.Flag('+') {
|
|
if l.Filepath != "" {
|
|
fmt.Fprintf(w, "%v:%v:%v[%v]", l.Filepath, l.Line, l.Column, l.Rune)
|
|
} else {
|
|
fmt.Fprintf(w, "%v:%v[%v]", l.Line, l.Column, l.Rune)
|
|
}
|
|
} else {
|
|
if l.Filepath != "" {
|
|
fmt.Fprintf(w, "%v:%v:%v", l.Filepath, l.Line, l.Column)
|
|
} else {
|
|
fmt.Fprintf(w, "%v:%v", l.Line, l.Column)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Source describes a start and end range in the source code
|
|
type Source struct {
|
|
S, E Location
|
|
}
|
|
|
|
// IsValid returns true if the source is valid
|
|
func (s Source) IsValid() bool {
|
|
return s.S.Line != 0 && s.S.Column != 0 && s.E.Line != 0 && s.E.Column != 0
|
|
}
|
|
|
|
// Format implements the fmt.Formatter interface
|
|
func (s Source) Format(w fmt.State, verb rune) {
|
|
if w.Flag('+') {
|
|
fmt.Fprint(w, "[")
|
|
s.S.Format(w, verb)
|
|
fmt.Fprint(w, " - ")
|
|
s.E.Format(w, verb)
|
|
fmt.Fprint(w, "]")
|
|
} else {
|
|
s.S.Format(w, verb)
|
|
}
|
|
}
|
|
|
|
// Token describes a parsed token
|
|
type Token struct {
|
|
Kind Kind
|
|
Runes []rune
|
|
Source Source
|
|
}
|
|
|
|
// Format implements the fmt.Formatter interface
|
|
func (t Token) Format(w fmt.State, verb rune) {
|
|
fmt.Fprint(w, "[")
|
|
t.Source.Format(w, verb)
|
|
fmt.Fprint(w, " ")
|
|
fmt.Fprint(w, t.Kind)
|
|
fmt.Fprint(w, " ")
|
|
fmt.Fprint(w, string(t.Runes))
|
|
fmt.Fprint(w, "]")
|
|
}
|