github.com/team-ide/go-dialect@v1.9.20/dialect/statement_parser.go (about)

     1  package dialect
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  func statementParse(content string) (statement *RootStatement, err error) {
     8  	parser := &StatementParser{
     9  		content: strings.Split(content, ""),
    10  	}
    11  	statement, err = parser.parse()
    12  	return
    13  }
    14  
    15  type StatementParser struct {
    16  	content       []string
    17  	contentLen    int
    18  	curIndex      int // 当前索引
    19  	curRowStart   int // 当前行开始索引
    20  	curStr        string
    21  	curStatement  Statement
    22  	curParent     Statement
    23  	curRow        int // 当前行
    24  	curCol        int // 当前列
    25  	bracketLevel  int // “{}” 层级
    26  	braceLevel    int // “[]” 层级
    27  	inStringLevel int
    28  	inStringPack  string
    29  
    30  	curIf     *IfStatement
    31  	curElseIf *ElseIfStatement
    32  	curElse   *ElseStatement
    33  }
    34  
    35  func (this_ *StatementParser) reset() {
    36  
    37  	this_.contentLen = len(this_.content)
    38  	this_.curRow = 0
    39  	this_.curCol = 0
    40  	this_.curIndex = 0
    41  	this_.curRowStart = 0
    42  	this_.curStr = ""
    43  	this_.curParent = nil
    44  	this_.curStatement = nil
    45  	this_.bracketLevel = 0
    46  	this_.braceLevel = 0
    47  
    48  	this_.inStringLevel = 0
    49  	this_.inStringPack = ""
    50  
    51  	this_.curIf = nil
    52  	this_.curElseIf = nil
    53  	this_.curElse = nil
    54  }
    55  
    56  func (this_ *StatementParser) parse() (statement *RootStatement, err error) {
    57  	statement = &RootStatement{
    58  		AbstractStatement: &AbstractStatement{},
    59  	}
    60  	this_.reset()
    61  	this_.curParent = statement
    62  	err = this_.parseStr()
    63  	if err != nil {
    64  		return
    65  	}
    66  	return
    67  }
    68  
    69  func (this_ *StatementParser) parseStr() (err error) {
    70  	if this_.contentLen <= this_.curIndex {
    71  		if this_.curStr != "" {
    72  			var statements []Statement
    73  			statements, err = parseTextStatement(this_.curStr, this_.curParent)
    74  			if err != nil {
    75  				return
    76  			}
    77  			*this_.curParent.GetChildren() = append(*this_.curParent.GetChildren(), statements...)
    78  		}
    79  		return
    80  	}
    81  	var char = this_.content[this_.curIndex]
    82  	var lastChar string
    83  	if this_.curIndex > 0 {
    84  		lastChar = this_.content[this_.curIndex-1]
    85  	}
    86  	this_.curStr += char
    87  
    88  	var stringPackChars = []string{"\"", "'"}
    89  	packCharIndex := StringsIndex(stringPackChars, char)
    90  	if packCharIndex >= 0 {
    91  		// inStringLevel == 0 表示 不在 字符串 包装 中
    92  		if this_.inStringLevel == 0 {
    93  			this_.inStringPack = stringPackChars[packCharIndex]
    94  			// 字符串包装层级 +1
    95  			this_.inStringLevel++
    96  		} else {
    97  			// 如果有转义符号 类似 “\'”,“\"”
    98  			if lastChar == "\\" {
    99  			} else if lastChar == this_.inStringPack {
   100  				// 如果 前一个字符 与字符串包装字符一致
   101  				this_.inStringLevel--
   102  			} else {
   103  				// 字符串包装层级 -1
   104  				this_.inStringLevel--
   105  			}
   106  		}
   107  	}
   108  
   109  	if this_.inStringLevel == 0 {
   110  		err = this_.checkStatement()
   111  		if err != nil {
   112  			return
   113  		}
   114  	}
   115  	if char == "\n" {
   116  		this_.curRow++
   117  		this_.curCol = 0
   118  	} else {
   119  		this_.curCol++
   120  	}
   121  	this_.curIndex++
   122  	err = this_.parseStr()
   123  	return
   124  }
   125  
   126  func (this_ *StatementParser) checkStatement() (err error) {
   127  	var startIndex int
   128  	var endIndex int
   129  	defer func() {
   130  		if startIndex != endIndex {
   131  			this_.curStr = ""
   132  		}
   133  	}()
   134  
   135  	switch this_.curStatement.(type) {
   136  	case *IfStatement:
   137  		if startIndex, endIndex, err = this_.checkElseIfStatement(); err != nil || startIndex != endIndex {
   138  			return
   139  		}
   140  		if startIndex, endIndex, err = this_.checkElseStatement(); err != nil || startIndex != endIndex {
   141  			return
   142  		}
   143  		if startIndex, endIndex, err = this_.checkIfEndStatement(); err != nil || startIndex != endIndex {
   144  			return
   145  		}
   146  		break
   147  	case *ElseIfStatement:
   148  		if startIndex, endIndex, err = this_.checkElseIfStatement(); err != nil || startIndex != endIndex {
   149  			return
   150  		}
   151  		if startIndex, endIndex, err = this_.checkElseStatement(); err != nil || startIndex != endIndex {
   152  			return
   153  		}
   154  		if startIndex, endIndex, err = this_.checkIfEndStatement(); err != nil || startIndex != endIndex {
   155  			return
   156  		}
   157  		break
   158  	case *ElseStatement:
   159  		if startIndex, endIndex, err = this_.checkIfEndStatement(); err != nil || startIndex != endIndex {
   160  			return
   161  		}
   162  		break
   163  	default:
   164  		if startIndex, endIndex, err = this_.checkIfStatement(); err != nil || startIndex != endIndex {
   165  			return
   166  		}
   167  	}
   168  
   169  	return
   170  }