File: parse_include_preprocessor_line.go

package info (click to toggle)
c2go 0.26.11-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,052 kB
  • sloc: ansic: 6,037; sh: 82; makefile: 5
file content (47 lines) | stat: -rw-r--r-- 1,033 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package preprocessor

import (
	"fmt"
	"strconv"
	"strings"
)

// typically parse that line:
// # 11 "/usr/include/x86_64-linux-gnu/gnu/stubs.h" 2 3 4
func parseIncludePreprocessorLine(line string) (item *entity, err error) {
	if line[0] != '#' {
		err = fmt.Errorf("Cannot parse: first symbol is not # in line %s", line)
		return
	}
	i := strings.Index(line, "\"")
	if i < 0 {
		err = fmt.Errorf("First index is not correct on line %s", line)
		return
	}
	l := strings.LastIndex(line, "\"")
	if i >= l {
		err = fmt.Errorf("Not allowable positions of symbol \" (%d and %d) in line : %s", i, l, line)
		return
	}

	pos, err := strconv.ParseInt(strings.TrimSpace(line[1:i]), 10, 64)
	if err != nil {
		err = fmt.Errorf("Cannot parse position in source : %v", err)
		return
	}

	if l+1 < len(line) {
		item = &entity{
			positionInSource: int(pos),
			include:          line[i+1 : l],
			other:            line[l+1:],
		}
	} else {
		item = &entity{
			positionInSource: int(pos),
			include:          line[i+1 : l],
		}
	}

	return
}