ini_parser.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. package ini
  2. import (
  3. "fmt"
  4. "io"
  5. )
  6. // ParseState represents the current state of the parser.
  7. type ParseState uint
  8. // State enums for the parse table
  9. const (
  10. InvalidState ParseState = iota
  11. // stmt -> value stmt'
  12. StatementState
  13. // stmt' -> MarkComplete | op stmt
  14. StatementPrimeState
  15. // value -> number | string | boolean | quoted_string
  16. ValueState
  17. // section -> [ section'
  18. OpenScopeState
  19. // section' -> value section_close
  20. SectionState
  21. // section_close -> ]
  22. CloseScopeState
  23. // SkipState will skip (NL WS)+
  24. SkipState
  25. // SkipTokenState will skip any token and push the previous
  26. // state onto the stack.
  27. SkipTokenState
  28. // comment -> # comment' | ; comment'
  29. // comment' -> MarkComplete | value
  30. CommentState
  31. // MarkComplete state will complete statements and move that
  32. // to the completed AST list
  33. MarkCompleteState
  34. // TerminalState signifies that the tokens have been fully parsed
  35. TerminalState
  36. )
  37. // parseTable is a state machine to dictate the grammar above.
  38. var parseTable = map[ASTKind]map[TokenType]ParseState{
  39. ASTKindStart: {
  40. TokenLit: StatementState,
  41. TokenSep: OpenScopeState,
  42. TokenWS: SkipTokenState,
  43. TokenNL: SkipTokenState,
  44. TokenComment: CommentState,
  45. TokenNone: TerminalState,
  46. },
  47. ASTKindCommentStatement: {
  48. TokenLit: StatementState,
  49. TokenSep: OpenScopeState,
  50. TokenWS: SkipTokenState,
  51. TokenNL: SkipTokenState,
  52. TokenComment: CommentState,
  53. TokenNone: MarkCompleteState,
  54. },
  55. ASTKindExpr: {
  56. TokenOp: StatementPrimeState,
  57. TokenLit: ValueState,
  58. TokenSep: OpenScopeState,
  59. TokenWS: ValueState,
  60. TokenNL: SkipState,
  61. TokenComment: CommentState,
  62. TokenNone: MarkCompleteState,
  63. },
  64. ASTKindEqualExpr: {
  65. TokenLit: ValueState,
  66. TokenSep: ValueState,
  67. TokenOp: ValueState,
  68. TokenWS: SkipTokenState,
  69. TokenNL: SkipState,
  70. TokenNone: SkipState,
  71. },
  72. ASTKindStatement: {
  73. TokenLit: SectionState,
  74. TokenSep: CloseScopeState,
  75. TokenWS: SkipTokenState,
  76. TokenNL: SkipTokenState,
  77. TokenComment: CommentState,
  78. TokenNone: MarkCompleteState,
  79. },
  80. ASTKindExprStatement: {
  81. TokenLit: ValueState,
  82. TokenSep: ValueState,
  83. TokenOp: ValueState,
  84. TokenWS: ValueState,
  85. TokenNL: MarkCompleteState,
  86. TokenComment: CommentState,
  87. TokenNone: TerminalState,
  88. TokenComma: SkipState,
  89. },
  90. ASTKindSectionStatement: {
  91. TokenLit: SectionState,
  92. TokenOp: SectionState,
  93. TokenSep: CloseScopeState,
  94. TokenWS: SectionState,
  95. TokenNL: SkipTokenState,
  96. },
  97. ASTKindCompletedSectionStatement: {
  98. TokenWS: SkipTokenState,
  99. TokenNL: SkipTokenState,
  100. TokenLit: StatementState,
  101. TokenSep: OpenScopeState,
  102. TokenComment: CommentState,
  103. TokenNone: MarkCompleteState,
  104. },
  105. ASTKindSkipStatement: {
  106. TokenLit: StatementState,
  107. TokenSep: OpenScopeState,
  108. TokenWS: SkipTokenState,
  109. TokenNL: SkipTokenState,
  110. TokenComment: CommentState,
  111. TokenNone: TerminalState,
  112. },
  113. }
  114. // ParseAST will parse input from an io.Reader using
  115. // an LL(1) parser.
  116. func ParseAST(r io.Reader) ([]AST, error) {
  117. lexer := iniLexer{}
  118. tokens, err := lexer.Tokenize(r)
  119. if err != nil {
  120. return []AST{}, err
  121. }
  122. return parse(tokens)
  123. }
  124. // ParseASTBytes will parse input from a byte slice using
  125. // an LL(1) parser.
  126. func ParseASTBytes(b []byte) ([]AST, error) {
  127. lexer := iniLexer{}
  128. tokens, err := lexer.tokenize(b)
  129. if err != nil {
  130. return []AST{}, err
  131. }
  132. return parse(tokens)
  133. }
  134. func parse(tokens []Token) ([]AST, error) {
  135. start := Start
  136. stack := newParseStack(3, len(tokens))
  137. stack.Push(start)
  138. s := newSkipper()
  139. loop:
  140. for stack.Len() > 0 {
  141. k := stack.Pop()
  142. var tok Token
  143. if len(tokens) == 0 {
  144. // this occurs when all the tokens have been processed
  145. // but reduction of what's left on the stack needs to
  146. // occur.
  147. tok = emptyToken
  148. } else {
  149. tok = tokens[0]
  150. }
  151. step := parseTable[k.Kind][tok.Type()]
  152. if s.ShouldSkip(tok) {
  153. // being in a skip state with no tokens will break out of
  154. // the parse loop since there is nothing left to process.
  155. if len(tokens) == 0 {
  156. break loop
  157. }
  158. // if should skip is true, we skip the tokens until should skip is set to false.
  159. step = SkipTokenState
  160. }
  161. switch step {
  162. case TerminalState:
  163. // Finished parsing. Push what should be the last
  164. // statement to the stack. If there is anything left
  165. // on the stack, an error in parsing has occurred.
  166. if k.Kind != ASTKindStart {
  167. stack.MarkComplete(k)
  168. }
  169. break loop
  170. case SkipTokenState:
  171. // When skipping a token, the previous state was popped off the stack.
  172. // To maintain the correct state, the previous state will be pushed
  173. // onto the stack.
  174. stack.Push(k)
  175. case StatementState:
  176. if k.Kind != ASTKindStart {
  177. stack.MarkComplete(k)
  178. }
  179. expr := newExpression(tok)
  180. stack.Push(expr)
  181. case StatementPrimeState:
  182. if tok.Type() != TokenOp {
  183. stack.MarkComplete(k)
  184. continue
  185. }
  186. if k.Kind != ASTKindExpr {
  187. return nil, NewParseError(
  188. fmt.Sprintf("invalid expression: expected Expr type, but found %T type", k),
  189. )
  190. }
  191. k = trimSpaces(k)
  192. expr := newEqualExpr(k, tok)
  193. stack.Push(expr)
  194. case ValueState:
  195. // ValueState requires the previous state to either be an equal expression
  196. // or an expression statement.
  197. switch k.Kind {
  198. case ASTKindEqualExpr:
  199. // assigning a value to some key
  200. k.AppendChild(newExpression(tok))
  201. stack.Push(newExprStatement(k))
  202. case ASTKindExpr:
  203. k.Root.raw = append(k.Root.raw, tok.Raw()...)
  204. stack.Push(k)
  205. case ASTKindExprStatement:
  206. root := k.GetRoot()
  207. children := root.GetChildren()
  208. if len(children) == 0 {
  209. return nil, NewParseError(
  210. fmt.Sprintf("invalid expression: AST contains no children %s", k.Kind),
  211. )
  212. }
  213. rhs := children[len(children)-1]
  214. if rhs.Root.ValueType != QuotedStringType {
  215. rhs.Root.ValueType = StringType
  216. rhs.Root.raw = append(rhs.Root.raw, tok.Raw()...)
  217. }
  218. children[len(children)-1] = rhs
  219. root.SetChildren(children)
  220. stack.Push(k)
  221. }
  222. case OpenScopeState:
  223. if !runeCompare(tok.Raw(), openBrace) {
  224. return nil, NewParseError("expected '['")
  225. }
  226. // If OpenScopeState is not at the start, we must mark the previous ast as complete
  227. //
  228. // for example: if previous ast was a skip statement;
  229. // we should mark it as complete before we create a new statement
  230. if k.Kind != ASTKindStart {
  231. stack.MarkComplete(k)
  232. }
  233. stmt := newStatement()
  234. stack.Push(stmt)
  235. case CloseScopeState:
  236. if !runeCompare(tok.Raw(), closeBrace) {
  237. return nil, NewParseError("expected ']'")
  238. }
  239. k = trimSpaces(k)
  240. stack.Push(newCompletedSectionStatement(k))
  241. case SectionState:
  242. var stmt AST
  243. switch k.Kind {
  244. case ASTKindStatement:
  245. // If there are multiple literals inside of a scope declaration,
  246. // then the current token's raw value will be appended to the Name.
  247. //
  248. // This handles cases like [ profile default ]
  249. //
  250. // k will represent a SectionStatement with the children representing
  251. // the label of the section
  252. stmt = newSectionStatement(tok)
  253. case ASTKindSectionStatement:
  254. k.Root.raw = append(k.Root.raw, tok.Raw()...)
  255. stmt = k
  256. default:
  257. return nil, NewParseError(
  258. fmt.Sprintf("invalid statement: expected statement: %v", k.Kind),
  259. )
  260. }
  261. stack.Push(stmt)
  262. case MarkCompleteState:
  263. if k.Kind != ASTKindStart {
  264. stack.MarkComplete(k)
  265. }
  266. if stack.Len() == 0 {
  267. stack.Push(start)
  268. }
  269. case SkipState:
  270. stack.Push(newSkipStatement(k))
  271. s.Skip()
  272. case CommentState:
  273. if k.Kind == ASTKindStart {
  274. stack.Push(k)
  275. } else {
  276. stack.MarkComplete(k)
  277. }
  278. stmt := newCommentStatement(tok)
  279. stack.Push(stmt)
  280. default:
  281. return nil, NewParseError(
  282. fmt.Sprintf("invalid state with ASTKind %v and TokenType %v",
  283. k, tok.Type()))
  284. }
  285. if len(tokens) > 0 {
  286. tokens = tokens[1:]
  287. }
  288. }
  289. // this occurs when a statement has not been completed
  290. if stack.top > 1 {
  291. return nil, NewParseError(fmt.Sprintf("incomplete ini expression"))
  292. }
  293. // returns a sublist which excludes the start symbol
  294. return stack.List(), nil
  295. }
  296. // trimSpaces will trim spaces on the left and right hand side of
  297. // the literal.
  298. func trimSpaces(k AST) AST {
  299. // trim left hand side of spaces
  300. for i := 0; i < len(k.Root.raw); i++ {
  301. if !isWhitespace(k.Root.raw[i]) {
  302. break
  303. }
  304. k.Root.raw = k.Root.raw[1:]
  305. i--
  306. }
  307. // trim right hand side of spaces
  308. for i := len(k.Root.raw) - 1; i >= 0; i-- {
  309. if !isWhitespace(k.Root.raw[i]) {
  310. break
  311. }
  312. k.Root.raw = k.Root.raw[:len(k.Root.raw)-1]
  313. }
  314. return k
  315. }