コード例 #1
0
ファイル: source.go プロジェクト: keysonZZZ/kmg
func RemoteEmptyLine(src []byte) []byte {
	r := kmgGoReader.NewReader(src, nil)
	outBuf := &bytes.Buffer{}
	r.ReadAllSpaceWithoutLineBreak()
	if r.IsEof() {
		return []byte{}
	}
	b := r.ReadByte()
	if b != '\n' {
		outBuf.Write(r.BufToCurrent(0))
	}
	for {
		if r.IsEof() {
			return outBuf.Bytes()
		}
		b := r.ReadByte()
		if b == '\n' {
			startPos := r.Pos() - 1 // 包括那个\n
			r.ReadAllSpaceWithoutLineBreak()
			if r.IsEof() {
				outBuf.WriteByte('\n')
				return outBuf.Bytes()
			}
			b := r.ReadByte()
			if b == '\n' {
				r.UnreadByte()
			} else {
				outBuf.Write(r.BufToCurrent(startPos))
			}
		} else {
			outBuf.WriteByte(b)
		}
	}
}
コード例 #2
0
ファイル: kmgGoParser.go プロジェクト: keysonZZZ/kmg
func parseFile(pkgPath string, path string, pkg *Package) *File {
	gofile := &File{
		PackagePath:    pkgPath,
		ImportMap:      map[string]bool{},
		AliasImportMap: map[string]string{},
		Pkg:            pkg,
	}
	content := kmgFile.MustReadFile(path)
	posFile := kmgGoReader.NewPosFile(path, content)
	content = goSourceRemoveComment(content, posFile)
	r := kmgGoReader.NewReader(content, posFile)

	r.ReadAllSpace()
	r.MustReadMatch(tokenPackage)
	r.ReadUntilByte('\n')
	for {
		if r.IsEof() {
			return gofile //没有 import 正确情况
		}
		r.ReadAllSpace()
		if r.IsMatchAfter(tokenImport) {
			gofile.readImport(r)
			continue
		}
		break
	}
	for {
		switch {
		case r.IsEof():
			return gofile
		case r.IsMatchAfter(tokenFunc):
			funcDecl := gofile.readGoFunc(r)
			if funcDecl.GetKind() == Func {
				gofile.FuncList = append(gofile.FuncList, funcDecl)
			} else {
				gofile.MethodList = append(gofile.MethodList, funcDecl)
			}
		case r.IsMatchAfter(tokenType):
			//r.ReadUntilByte('\n')
			gofile.readGoType(r)
		case r.IsMatchAfter(tokenVar):
			gofile.readGoVar(r)
		case r.IsMatchAfter(tokenConst):
			gofile.readGoConst(r)
		// 有一些没有分析的代码,里面可能包含import,此处先简单绕过.
		case r.IsMatchAfter(tokenDoubleQuate) || r.IsMatchAfter(tokenGraveAccent):
			MustReadGoString(r)
			//fmt.Println(string(ret))
		case r.IsMatchAfter(tokenSingleQuate):
			mustReadGoChar(r)
		default:
			r.ReadByte()
		}
	}
}
コード例 #3
0
func goSourceRemoveComment(in []byte, filePos *kmgGoReader.FilePos) (out []byte) {
	r := kmgGoReader.NewReader(in, filePos)
	buf := &noGrowBuf{
		buf: make([]byte, len(in)),
		pos: 0,
	}
	for {
		if r.IsEof() {
			return buf.buf
		}
		b := r.ReadByte()
		switch b {
		case '/':
			r.UnreadByte()
			if r.IsMatchAfter(tokenSlashStar) {
				thisBuf := r.ReadUntilString(tokenStarSlash)
				// 保留换行
				commentReader := kmgGoReader.NewReader(thisBuf, nil)
				for {
					if commentReader.IsEof() {
						break
					}
					b := commentReader.ReadByte()
					if b == '\n' {
						buf.WriteByte('\n')
					} else {
						buf.WriteByte(' ')
					}
				}
			} else if r.IsMatchAfter(tokenDoubleSlash) {
				thisBuf := r.ReadUntilByte('\n')
				buf.Write(bytes.Repeat([]byte{' '}, len(thisBuf)-1))
				buf.WriteByte('\n')
			} else {
				buf.WriteByte(r.ReadByte())
			}
		case '"', '`':
			r.UnreadByte()
			startPos := r.Pos()
			//fmt.Println("string start "+r.GetFileLineInfo())
			MustReadGoString(r)
			buf.Write(r.BufToCurrent(startPos))
		case '\'':
			r.UnreadByte()
			startPos := r.Pos()
			mustReadGoChar(r)
			buf.Write(r.BufToCurrent(startPos))
		default:
			buf.WriteByte(b)
		}
		/*
			switch {
			case r.IsEof():
				return outBuf.Bytes()
			case r.IsMatchAfter(tokenSlashStar):
				thisBuf := r.ReadUntilString(tokenStarSlash)
				// 保留换行
				commentReader := kmgGoReader.NewReader(thisBuf, nil)
				for {
					if commentReader.IsEof() {
						break
					}
					b := commentReader.ReadByte()
					if b == '\n' {
						outBuf.WriteByte('\n')
					} else {
						outBuf.WriteByte(' ')
					}
				}
			case r.IsMatchAfter(tokenDoubleSlash):
				thisBuf := r.ReadUntilByte('\n')
				outBuf.Write(bytes.Repeat([]byte{' '}, len(thisBuf)-1))
				outBuf.WriteByte('\n')
			case r.IsMatchAfter(tokenDoubleQuate) || r.IsMatchAfter(tokenGraveAccent):
				startPos := r.Pos()
				//fmt.Println("string start "+r.GetFileLineInfo())
				mustReadGoString(r)
				outBuf.Write(r.BufToCurrent(startPos))
			case r.IsMatchAfter(tokenSingleQuate):
				startPos := r.Pos()
				mustReadGoChar(r)
				outBuf.Write(r.BufToCurrent(startPos))
			default:
				outBuf.WriteByte(r.ReadByte())
			}
		*/
	}
}