...

Source file src/cmd/compile/internal/syntax/testing.go

Documentation: cmd/compile/internal/syntax

     1  // Copyright 2020 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  // This file implements testing support.
     6  
     7  package syntax
     8  
     9  import (
    10  	"io"
    11  	"regexp"
    12  )
    13  
    14  // CommentsDo parses the given source and calls the provided handler for each
    15  // comment or error. If the text provided to handler starts with a '/' it is
    16  // the comment text; otherwise it is the error message.
    17  func CommentsDo(src io.Reader, handler func(line, col uint, text string)) {
    18  	var s scanner
    19  	s.init(src, handler, comments)
    20  	for s.tok != _EOF {
    21  		s.next()
    22  	}
    23  }
    24  
    25  // CommentMap collects all comments in the given src with comment text
    26  // that matches the supplied regular expression rx and returns them as
    27  // []Error lists in a map indexed by line number. The comment text is
    28  // the comment with any comment markers ("//", "/*", or "*/") stripped.
    29  // The position for each Error is the position of the token immediately
    30  // preceding the comment and the Error message is the comment text,
    31  // with all comments that are on the same line collected in a slice, in
    32  // source order. If there is no preceding token (the matching comment
    33  // appears at the beginning of the file), then the recorded position
    34  // is unknown (line, col = 0, 0). If there are no matching comments,
    35  // the result is nil.
    36  func CommentMap(src io.Reader, rx *regexp.Regexp) (res map[uint][]Error) {
    37  	// position of previous token
    38  	var base *PosBase
    39  	var prev struct{ line, col uint }
    40  
    41  	var s scanner
    42  	s.init(src, func(_, _ uint, text string) {
    43  		if text[0] != '/' {
    44  			return // not a comment, ignore
    45  		}
    46  		if text[1] == '*' {
    47  			text = text[:len(text)-2] // strip trailing */
    48  		}
    49  		text = text[2:] // strip leading // or /*
    50  		if rx.MatchString(text) {
    51  			pos := MakePos(base, prev.line, prev.col)
    52  			err := Error{pos, text}
    53  			if res == nil {
    54  				res = make(map[uint][]Error)
    55  			}
    56  			res[prev.line] = append(res[prev.line], err)
    57  		}
    58  	}, comments)
    59  
    60  	for s.tok != _EOF {
    61  		s.next()
    62  		if s.tok == _Semi && s.lit != "semicolon" {
    63  			continue // ignore automatically inserted semicolons
    64  		}
    65  		prev.line, prev.col = s.line, s.col
    66  	}
    67  
    68  	return
    69  }
    70  

View as plain text