cstruct

package module
v0.0.0-...-3e2172a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 9, 2020 License: MIT Imports: 6 Imported by: 0

README

cstruct-go

解析c语言结构体

获取包

go get github.com/geange/cstruct-go
example
package main

import (
	"bytes"
	"fmt"
	"github.com/geange/cstruct-go"
)

func main() {
	buf := bytes.NewBufferString(nestedText)
	lex, err := cstruct.NewLexer(buf)
	if err != nil {
		panic(err)
	}

	list, err := lex.AllStructure()
	if err != nil {
		panic(err)
	}

	for _, item := range list {
		fmt.Println(item.Name)
		for _, v := range item.FieldSet {
			fmt.Println(v.Name, v.Type, v.Size)
		}
	}
}

var (
	nestedText = `typedef struct{
    u32 ITOW;
    u16 Week;
    u16 VITOW;
}UTC_t;

typedef struct
{
    byte CustomData[16];
    UTC_t StartTime;
    UTC_t EndTime;
    u16 StartVoltage;
    u16 EndVoltage;
    u8  StartPoint;
    u8  EndPoint;
    u8  CurrentPoint;
    u8  Status;
} MSG_RouteData_t;`
)

Documentation

Index

Constants

View Source
const (
	// C语言中的数据类型
	BYTE    = FieldType("byte")
	UInt8   = FieldType("uint8")
	UInt16  = FieldType("uint16")
	UInt32  = FieldType("uint32")
	UInt64  = FieldType("uint64")
	Int8    = FieldType("int8")
	Int16   = FieldType("int16")
	Int32   = FieldType("int32")
	Int64   = FieldType("int64")
	Float32 = FieldType("float32")
	Float64 = FieldType("float64")
	// string和bytes的区别是,bytes解析出来后是base64编码,string是asc/utf-8编码
	Bytes  = FieldType("bytes")
	String = FieldType("string")
	Hex    = FieldType("hex")
	// 内部结构体
	Struct = FieldType("struct")
	Array  = FieldType("array")

	LEN0 = 0
	LEN1 = 1
	LEN2 = 2
	LEN4 = 4
	LEN8 = 8
)
View Source
const (
	TFloat32 = TokenType(iota)
	TFloat64
	TInt64
	TInt32
	TInt16
	TInt8
	TUint64
	TUint32
	TUint16
	TUint8
	TByte
	TLineEnd
	TStruct
	TType
	TTypedef
	TLeftBrace
	TRightBrace
	TLeftBracket
	TRightBracket
	TInteger
	TFloatingPoint
	TString
)

Variables

View Source
var (
	ReserveWords = []string{
		"auto", "break", "case", "byte", "const", "continue",
		"default", "do", "double", "else", "enum", "extern",
		"float", "for", "goto", "if", "int", "long",
		"s64", "s32", "s16", "s8",
		"u64", "u32", "u16", "u8",
		"register", "return", "short", "signed", "sizeof", "static",
		"struct", "switch", "typedef", "union", "unsigned", "void",
		"volatile", "while",
	}

	IntegerWords = []string{
		"s64", "s32", "s16", "s8",
		"u64", "u32", "u16", "u8",
	}

	FloatingPointWords = []string{
		"float", "double",
	}

	TokenTypeMap = map[TokenType]string{
		TFloat32:       "float32",
		TFloat64:       "float64",
		TInt64:         "int64",
		TInt32:         "int32",
		TInt16:         "int16",
		TInt8:          "int8",
		TUint64:        "uint64",
		TUint32:        "uint32",
		TUint16:        "uint16",
		TUint8:         "uint8",
		TLineEnd:       "line_end",
		TStruct:        "struct",
		TType:          "type",
		TTypedef:       "typedef",
		TLeftBrace:     "left_brace",
		TRightBrace:    "right_brace",
		TLeftBracket:   "left_bracket",
		TRightBracket:  "right_bracket",
		TInteger:       "integer",
		TFloatingPoint: "floating_point",
		TString:        "string",
		TByte:          "byte",
	}
)

Functions

func Format

func Format(format string) error

func IN

func IN(f string, fields ...string) bool

func ToUnderLine

func ToUnderLine(src string) string

Types

type BaseArrayV1

type BaseArrayV1 struct {
	// contains filtered or unexported fields
}

func (*BaseArrayV1) Copy

func (b *BaseArrayV1) Copy() StatementV1

func (*BaseArrayV1) Flat

func (b *BaseArrayV1) Flat() ([]StatementV1, error)

func (*BaseArrayV1) Name

func (b *BaseArrayV1) Name() string

func (*BaseArrayV1) SetFieldName

func (b *BaseArrayV1) SetFieldName(name string)

func (*BaseArrayV1) Type

func (b *BaseArrayV1) Type() FieldType

type BaseStructV1

type BaseStructV1 struct {
	// contains filtered or unexported fields
}

func (*BaseStructV1) ClassName

func (b *BaseStructV1) ClassName() string

func (*BaseStructV1) Copy

func (b *BaseStructV1) Copy() StatementV1

func (*BaseStructV1) Flat

func (b *BaseStructV1) Flat() ([]StatementV1, error)

func (*BaseStructV1) Name

func (b *BaseStructV1) Name() string

func (*BaseStructV1) SetFieldName

func (b *BaseStructV1) SetFieldName(name string)

func (*BaseStructV1) Type

func (b *BaseStructV1) Type() FieldType

type BaseV1

type BaseV1 struct {
	// contains filtered or unexported fields
}

func (*BaseV1) Copy

func (b *BaseV1) Copy() StatementV1

func (*BaseV1) Flat

func (b *BaseV1) Flat() ([]StatementV1, error)

func (*BaseV1) Name

func (b *BaseV1) Name() string

func (*BaseV1) SetFieldName

func (b *BaseV1) SetFieldName(name string)

func (*BaseV1) Type

func (b *BaseV1) Type() FieldType

type Byte

type Byte struct {
	BaseV1
}

type CStruct

type CStruct struct {
	Name     string
	FieldSet FieldSet
}

func (*CStruct) FieldNameToLower

func (c *CStruct) FieldNameToLower()

func (*CStruct) ToStatement

func (c *CStruct) ToStatement() ([]Field, error)

type DelimiterToken

type DelimiterToken struct {
	// contains filtered or unexported fields
}

界符

func (*DelimiterToken) Type

func (d *DelimiterToken) Type() TokenType

func (*DelimiterToken) Value

func (d *DelimiterToken) Value() interface{}

type DigitToken

type DigitToken struct {
	// contains filtered or unexported fields
}

常数

func (*DigitToken) Type

func (d *DigitToken) Type() TokenType

func (*DigitToken) Value

func (d *DigitToken) Value() interface{}

type F32

type F32 struct {
	BaseV1
}

type F64

type F64 struct {
	BaseV1
}

type Field

type Field struct {
	Index uint      // 字段序号
	Name  string    // 字段名
	Type  FieldType // 字段类型
	Size  uint      // 字段长度
}

type FieldSet

type FieldSet []Field

func (FieldSet) Len

func (fs FieldSet) Len() int

func (FieldSet) Less

func (fs FieldSet) Less(i, j int) bool

func (FieldSet) Swap

func (fs FieldSet) Swap(i, j int)

type FieldType

type FieldType string

type LetterToken

type LetterToken struct {
	// contains filtered or unexported fields
}

标识符

func (*LetterToken) Type

func (l *LetterToken) Type() TokenType

func (*LetterToken) Value

func (l *LetterToken) Value() interface{}

type Lexer

type Lexer interface {
	Fetch() (Token, error)
	Next() (Token, error)
	Index(n int) (Token, error)
	CurrentIndex() int
	Statement() ([]Field, error)
	Structure() (*CStruct, error)
	AllStructure() ([]*CStruct, error)
	ExistStructure(name string) (*CStruct, bool)
}

func NewLexer

func NewLexer(reader io.Reader) (Lexer, error)

type LexerV1

type LexerV1 interface {
	Fetch() (Token, error)
	Next() (Token, error)
	Index(n int) (Token, error)
	CurrentIndex() int
	AllStruct() ([]BaseStructV1, error)
}

func NewLexerV1

func NewLexerV1(reader io.Reader) (LexerV1, error)

type OperatorToken

type OperatorToken struct {
}

运算符

type ReservedWordToken

type ReservedWordToken struct {
	// contains filtered or unexported fields
}

保留字

func (*ReservedWordToken) Type

func (r *ReservedWordToken) Type() TokenType

func (*ReservedWordToken) Value

func (r *ReservedWordToken) Value() interface{}

type S16

type S16 struct {
	BaseV1
}

type S32

type S32 struct {
	BaseV1
}

type S64

type S64 struct {
	BaseV1
}

type S8

type S8 struct {
	BaseV1
}

type Scanner

type Scanner interface {
	Format() error
	Fetch() (byte, error)
	Next() (byte, error)
	Index(n int) (byte, error)
	CurrentIndex() (int, error)
	ScanDigit() (Token, error)
	ScanLetter() (Token, error)
	Scan() ([]Token, error)
}

func NewScanner

func NewScanner(text []byte) Scanner

func NewScannerString

func NewScannerString(text string) Scanner

type Statement

type Statement struct {
}

type StatementV1

type StatementV1 interface {
	Type() FieldType
	Name() string
	SetFieldName(name string)
	Copy() StatementV1
}

type Token

type Token interface {
	Type() TokenType
	Value() interface{}
}

type TokenType

type TokenType int

type U16

type U16 struct {
	BaseV1
}

type U32

type U32 struct {
	BaseV1
}

type U64

type U64 struct {
	BaseV1
}

type U8

type U8 struct {
	BaseV1
}

Directories

Path Synopsis
example
v0
v1

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL