github.com/gopherd/gonum@v0.0.4/graph/formats/dot/internal/token/token.go (about)

     1  // Code generated by gocc; DO NOT EDIT.
     2  
     3  // This file is dual licensed under CC0 and The Gonum License.
     4  //
     5  // Copyright ©2017 The Gonum Authors. All rights reserved.
     6  // Use of this source code is governed by a BSD-style
     7  // license that can be found in the LICENSE file.
     8  //
     9  // Copyright ©2017 Robin Eklind.
    10  // This file is made available under a Creative Commons CC0 1.0
    11  // Universal Public Domain Dedication.
    12  
    13  package token
    14  
    15  import (
    16  	"fmt"
    17  )
    18  
    19  type Token struct {
    20  	Type
    21  	Lit []byte
    22  	Pos
    23  }
    24  
    25  type Type int
    26  
    27  const (
    28  	INVALID Type = iota
    29  	EOF
    30  )
    31  
    32  type Pos struct {
    33  	Offset int
    34  	Line   int
    35  	Column int
    36  }
    37  
    38  func (p Pos) String() string {
    39  	return fmt.Sprintf("Pos(offset=%d, line=%d, column=%d)", p.Offset, p.Line, p.Column)
    40  }
    41  
    42  type TokenMap struct {
    43  	typeMap []string
    44  	idMap   map[string]Type
    45  }
    46  
    47  func (m TokenMap) Id(tok Type) string {
    48  	if int(tok) < len(m.typeMap) {
    49  		return m.typeMap[tok]
    50  	}
    51  	return "unknown"
    52  }
    53  
    54  func (m TokenMap) Type(tok string) Type {
    55  	if typ, exist := m.idMap[tok]; exist {
    56  		return typ
    57  	}
    58  	return INVALID
    59  }
    60  
    61  func (m TokenMap) TokenString(tok *Token) string {
    62  	//TODO: refactor to print pos & token string properly
    63  	return fmt.Sprintf("%s(%d,%s)", m.Id(tok.Type), tok.Type, tok.Lit)
    64  }
    65  
    66  func (m TokenMap) StringType(typ Type) string {
    67  	return fmt.Sprintf("%s(%d)", m.Id(typ), typ)
    68  }
    69  
    70  var TokMap = TokenMap{
    71  	typeMap: []string{
    72  		"INVALID",
    73  		"$",
    74  		"{",
    75  		"}",
    76  		"empty",
    77  		"strict",
    78  		"graphx",
    79  		"digraph",
    80  		";",
    81  		"--",
    82  		"->",
    83  		"node",
    84  		"edge",
    85  		"[",
    86  		"]",
    87  		",",
    88  		"=",
    89  		"subgraph",
    90  		":",
    91  		"id",
    92  	},
    93  
    94  	idMap: map[string]Type{
    95  		"INVALID":  0,
    96  		"$":        1,
    97  		"{":        2,
    98  		"}":        3,
    99  		"empty":    4,
   100  		"strict":   5,
   101  		"graphx":   6,
   102  		"digraph":  7,
   103  		";":        8,
   104  		"--":       9,
   105  		"->":       10,
   106  		"node":     11,
   107  		"edge":     12,
   108  		"[":        13,
   109  		"]":        14,
   110  		",":        15,
   111  		"=":        16,
   112  		"subgraph": 17,
   113  		":":        18,
   114  		"id":       19,
   115  	},
   116  }