ini_parser.go 8.4 KB

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