github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/preprocessor/parse_include_preprocessor_line.go (about)

     1  package preprocessor
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  // typically parse that line:
    10  // # 11 "/usr/include/x86_64-linux-gnu/gnu/stubs.h" 2 3 4
    11  func parseIncludePreprocessorLine(line string) (item *entity, err error) {
    12  	if line[0] != '#' {
    13  		err = fmt.Errorf("cannot parse: first symbol is not # in line %s", line)
    14  		return
    15  	}
    16  	i := strings.Index(line, "\"")
    17  	if i < 0 {
    18  		err = fmt.Errorf("first index is not correct on line %s", line)
    19  		return
    20  	}
    21  	l := strings.LastIndex(line, "\"")
    22  	if i >= l {
    23  		err = fmt.Errorf("not allowable positions of symbol \" (%d and %d) in line : %s", i, l, line)
    24  		return
    25  	}
    26  
    27  	pos, err := strconv.ParseInt(strings.TrimSpace(line[1:i]), 10, 64)
    28  	if err != nil {
    29  		err = fmt.Errorf("cannot parse position in source : %v", err)
    30  		return
    31  	}
    32  
    33  	if l+1 < len(line) {
    34  		item = &entity{
    35  			positionInSource: int(pos),
    36  			include:          line[i+1 : l],
    37  			other:            line[l+1:],
    38  		}
    39  	} else {
    40  		item = &entity{
    41  			positionInSource: int(pos),
    42  			include:          line[i+1 : l],
    43  		}
    44  	}
    45  
    46  	return
    47  }