| 1 | //Copyright 2013 Vastech SA (PTY) LTD |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package ast |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "fmt" |
| 20 | "strings" |
| 21 | |
| 22 | "github.com/goccmack/gocc/internal/frontend/token" |
| 23 | ) |
| 24 | |
| 25 | type LexTokDef struct { |
| 26 | id string |
| 27 | pattern *LexPattern |
| 28 | } |
| 29 | |
| 30 | func NewLexTokDef(tokId, lexPattern interface{}) (*LexTokDef, error) { |
| 31 | tokDef := &LexTokDef{ |
| 32 | id: string(tokId.(*token.Token).Lit), |
| 33 | pattern: lexPattern.(*LexPattern), |
| 34 | } |
| 35 | return tokDef, nil |
| 36 | } |
| 37 | |
| 38 | func NewLexStringLitTokDef(tokId string) *LexTokDef { |
| 39 | runes := bytes.Runes([]byte(tokId)) |
| 40 | alt, _ := NewLexAlt(newLexCharLitFromRune(runes[0])) |
| 41 | for i := 1; i < len(runes); i++ { |
| 42 | alt, _ = AppendLexTerm(alt, newLexCharLitFromRune(runes[i])) |
| 43 | } |
| 44 | ptrn, _ := NewLexPattern(alt) |
| 45 | return &LexTokDef{ |
| 46 | id: tokId, |
| 47 | pattern: ptrn, |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func (*LexTokDef) RegDef() bool { |
| 52 | return false |
| 53 | } |
| 54 | |
| 55 | func (this *LexTokDef) String() string { |
| 56 | buf := new(strings.Builder) |
| 57 | fmt.Fprintf(buf, "%s : %s", this.id, this.pattern.String()) |
| 58 | return buf.String() |
| 59 | } |
| 60 | |
| 61 | func (this *LexTokDef) Id() string { |
| 62 | return this.id |
| 63 | } |
| 64 | |
| 65 | func (this *LexTokDef) LexPattern() *LexPattern { |
| 66 | return this.pattern |
| 67 | } |