github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/compile/internal/syntax/pos.go (about)

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package syntax
     6  
     7  // PosMax is the largest line or column value that can be represented without loss.
     8  // Incoming values (arguments) larger than PosMax will be set to PosMax.
     9  //
    10  // Keep this consistent with maxLineCol in go/scanner.
    11  const PosMax = 1 << 30
    12  
    13  // A Pos represents an absolute (line, col) source position
    14  // with a reference to position base for computing relative
    15  // (to a file, or line directive) position information.
    16  // Pos values are intentionally light-weight so that they
    17  // can be created without too much concern about space use.
    18  type Pos struct {
    19  	base      *PosBase
    20  	line, col uint32
    21  }
    22  
    23  // MakePos returns a new Pos for the given PosBase, line and column.
    24  func MakePos(base *PosBase, line, col uint) Pos
    25  
    26  func (pos Pos) Pos() Pos
    27  func (pos Pos) IsKnown() bool
    28  func (pos Pos) Base() *PosBase
    29  func (pos Pos) Line() uint
    30  func (pos Pos) Col() uint
    31  
    32  func (pos Pos) RelFilename() string
    33  
    34  func (pos Pos) RelLine() uint
    35  
    36  func (pos Pos) RelCol() uint
    37  
    38  // Cmp compares the positions p and q and returns a result r as follows:
    39  //
    40  //	r <  0: p is before q
    41  //	r == 0: p and q are the same position (but may not be identical)
    42  //	r >  0: p is after q
    43  //
    44  // If p and q are in different files, p is before q if the filename
    45  // of p sorts lexicographically before the filename of q.
    46  func (p Pos) Cmp(q Pos) int
    47  
    48  func (pos Pos) String() string
    49  
    50  // A PosBase represents the base for relative position information:
    51  // At position pos, the relative position is filename:line:col.
    52  type PosBase struct {
    53  	pos       Pos
    54  	filename  string
    55  	line, col uint32
    56  	trimmed   bool
    57  }
    58  
    59  // NewFileBase returns a new PosBase for the given filename.
    60  // A file PosBase's position is relative to itself, with the
    61  // position being filename:1:1.
    62  func NewFileBase(filename string) *PosBase
    63  
    64  // NewTrimmedFileBase is like NewFileBase, but allows specifying Trimmed.
    65  func NewTrimmedFileBase(filename string, trimmed bool) *PosBase
    66  
    67  // NewLineBase returns a new PosBase for a line directive "line filename:line:col"
    68  // relative to pos, which is the position of the character immediately following
    69  // the comment containing the line directive. For a directive in a line comment,
    70  // that position is the beginning of the next line (i.e., the newline character
    71  // belongs to the line comment).
    72  func NewLineBase(pos Pos, filename string, trimmed bool, line, col uint) *PosBase
    73  
    74  func (base *PosBase) IsFileBase() bool
    75  
    76  func (base *PosBase) Pos() (_ Pos)
    77  
    78  func (base *PosBase) Filename() string
    79  
    80  func (base *PosBase) Line() uint
    81  
    82  func (base *PosBase) Col() uint
    83  
    84  func (base *PosBase) Trimmed() bool