node.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. package sexp
  2. import (
  3. "fmt"
  4. "reflect"
  5. "strconv"
  6. "strings"
  7. )
  8. // The main and only AST structure. All fields are self explanatory, however
  9. // the way they are being formed needs explanation.
  10. //
  11. // A list node has empty value and non-nil children pointer, which is a
  12. // nil-terminated list of children nodes.
  13. //
  14. // A scalar node has nil children pointer.
  15. //
  16. // Take a look at this example:
  17. //
  18. // ((1 2) 3 4)
  19. //
  20. // will yield:
  21. //
  22. // Node{Children:
  23. // Node{Children:
  24. // Node{Value: "1", Next:
  25. // Node{Value: "2"}}, Next:
  26. // Node{Value: "3", Next:
  27. // Node{Value: "4"}}}}
  28. type Node struct {
  29. Location SourceLoc
  30. Value string
  31. Children *Node
  32. Next *Node
  33. }
  34. // Returns true if the node is a list (has children).
  35. func (n *Node) IsList() bool {
  36. return n.Children != nil
  37. }
  38. // Return true if the node is a scalar (has no children).
  39. func (n *Node) IsScalar() bool {
  40. return n.Children == nil
  41. }
  42. func (n *Node) String() string {
  43. return n.Value
  44. }
  45. // Returns the number of children nodes. Has O(N) complexity.
  46. func (n *Node) NumChildren() int {
  47. i := 0
  48. c := n.Children
  49. for c != nil {
  50. i++
  51. c = c.Next
  52. }
  53. return i
  54. }
  55. // Returns Nth child node. If node is not a list, it will return an error.
  56. func (n *Node) Nth(num int) (*Node, error) {
  57. if !n.IsList() {
  58. return nil, NewUnmarshalError(n, nil, "node is not a list")
  59. }
  60. i := 0
  61. for c := n.Children; c != nil; c = c.Next {
  62. if i == num {
  63. return c, nil
  64. }
  65. i++
  66. }
  67. num++
  68. return nil, NewUnmarshalError(n, nil,
  69. "cannot retrieve %d%s child node, %s",
  70. num, number_suffix(num),
  71. the_list_has_n_children(n.NumChildren()))
  72. }
  73. // Walk over children nodes, assuming they are key/value pairs. It returns error
  74. // if the iterable node is not a list or if any of its children is not a
  75. // key/value pair.
  76. func (n *Node) IterKeyValues(f func(k, v *Node) error) error {
  77. for c := n.Children; c != nil; c = c.Next {
  78. if !c.IsList() {
  79. return NewUnmarshalError(c, nil,
  80. "node is not a list, expected key/value pair")
  81. }
  82. // don't check for error here, because it's obvious that if the
  83. // node is a list (and the definition of the list is `Children
  84. // != nil`), it has at least one child
  85. k, _ := c.Nth(0)
  86. v, err := c.Nth(1)
  87. if err != nil {
  88. return err
  89. }
  90. err = f(k, v)
  91. if err != nil {
  92. return err
  93. }
  94. }
  95. return nil
  96. }
  97. type Unmarshaler interface {
  98. UnmarshalSexp(n *Node) error
  99. }
  100. // Unmarshals all children nodes of the node to pointer values. Applies the
  101. // same logic as Unmarshal. See description of the (*Node).Unmarshal method for
  102. // more details.
  103. func (n *Node) UnmarshalChildren(vals ...interface{}) (err error) {
  104. if len(vals) == 0 {
  105. return nil
  106. }
  107. // unmarshal all children of the node
  108. i := 0
  109. for c := n.Children; c != nil; c = c.Next {
  110. if i >= len(vals) {
  111. break
  112. }
  113. if vals[i] == nil {
  114. i++
  115. continue
  116. }
  117. if err := c.unmarshal(vals[i]); err != nil {
  118. return err
  119. }
  120. i++
  121. }
  122. // did we fullfil all the arguments?
  123. if i < len(vals) {
  124. if i == 0 {
  125. return NewUnmarshalError(n, nil,
  126. "node has no children, %d was requested",
  127. len(vals))
  128. }
  129. return NewUnmarshalError(n, nil,
  130. "node has %d children, %d was requested",
  131. i, len(vals))
  132. }
  133. return nil
  134. }
  135. // Unmarshals the node and its siblings to pointer values.
  136. //
  137. // The function expects pointers to values with arbitrary types. If one of the
  138. // arguments is not a pointer it will panic.
  139. //
  140. // It supports unmarshaling to the following types:
  141. // - all number types: int{8,16,32,64}, uint{8,16,32,64}, float{32,64}
  142. // - bool
  143. // - string
  144. // - arrays and slices of all supported types
  145. // - empty interfaces{}
  146. // - maps
  147. // - structs
  148. // - pointers to any of the supported types (only one level of indirection)
  149. // - any type which implements Unmarshaler
  150. //
  151. // Here's some details on unmarshaling semantics:
  152. // (u)ints: unmarshaled using strconv.ParseInt/strconv.ParseUint with base 10
  153. // only
  154. // floats: unmarshaled using strconv.ParseFloat
  155. // bool: works strictly on two values "true" or "false"
  156. // string: unmarshaled as is (keep in mind that lexer supports escape sequences)
  157. // arrays: uses up to len(array) elements, if there is a smaller amount of
  158. // elements, the rest is zeroed
  159. // slices: uses all elements appending them to the slice, however if the slice
  160. // was bigger than the amount of elements, it will reslice it to the
  161. // appropriate length
  162. // iface: only empty interfaces are supported, it will unmarshal AST to
  163. // a []interface{} or a string
  164. // map: when unmarshaling to the map, assumes this AST form:
  165. // `((key value) (key value) (key value))`, doesn't clear the map
  166. // before appending all the key value pairs
  167. // struct: uses the same AST form as the map, where `key` means `field`,
  168. // supports `sexp` tags (see description below), will try to match
  169. // name specified in the tag, the field name and the field name
  170. // ignoring the case in that order
  171. //
  172. // Struct tags have the form: "name,opt,opt". Special tag "-" means "skip me".
  173. // Supported options:
  174. // siblings: will use sibling nodes instead of children for unmarshaling
  175. // to an array or a slice.
  176. //
  177. // Important note: If the type implements Unmarshaler interface, it will use it
  178. // instead of applying default unmarshaling strategies described above.
  179. func (n *Node) Unmarshal(vals ...interface{}) (err error) {
  180. if len(vals) == 0 {
  181. return nil
  182. }
  183. // unmarshal the node itself
  184. if vals[0] != nil {
  185. if err := n.unmarshal(vals[0]); err != nil {
  186. return err
  187. }
  188. }
  189. // unmarshal node's siblings
  190. i := 1
  191. for s := n.Next; s != nil; s = s.Next {
  192. if i >= len(vals) {
  193. break
  194. }
  195. if vals[i] == nil {
  196. i++
  197. continue
  198. }
  199. if err := s.unmarshal(vals[i]); err != nil {
  200. return err
  201. }
  202. i++
  203. }
  204. // did we fullfil all the arguments?
  205. if i < len(vals) {
  206. if i == 1 {
  207. return NewUnmarshalError(n, nil,
  208. "node has no siblings, %d was requested",
  209. len(vals)-1)
  210. }
  211. return NewUnmarshalError(n, nil,
  212. "node has %d siblings, %d was requested",
  213. i-1, len(vals)-1)
  214. }
  215. return nil
  216. }
  217. type UnmarshalError struct {
  218. Type reflect.Type
  219. Node *Node
  220. message string
  221. }
  222. func NewUnmarshalError(n *Node, t reflect.Type, format string, args ...interface{}) *UnmarshalError {
  223. if len(args) == 0 {
  224. // simple hack to make it a bit faster in the case when no args
  225. // were provided
  226. return &UnmarshalError{
  227. Type: t,
  228. Node: n,
  229. message: format,
  230. }
  231. }
  232. return &UnmarshalError{
  233. Type: t,
  234. Node: n,
  235. message: fmt.Sprintf(format, args...),
  236. }
  237. }
  238. func (e *UnmarshalError) Error() string {
  239. args := []interface{}{e.message}
  240. format := "%s"
  241. if e.Node != nil {
  242. if e.Node.IsList() {
  243. format += " (list value)"
  244. } else {
  245. format += " (value: %q)"
  246. args = append(args, e.Node.Value)
  247. }
  248. }
  249. if e.Type != nil {
  250. format += " (type: %s)"
  251. args = append(args, e.Type)
  252. }
  253. return fmt.Sprintf(format, args...)
  254. }
  255. func (n *Node) unmarshal_error(t reflect.Type, format string, args ...interface{}) {
  256. panic(NewUnmarshalError(n, t, fmt.Sprintf(format, args...)))
  257. }
  258. func (n *Node) unmarshal_unmarshaler(v reflect.Value) bool {
  259. u, ok := v.Interface().(Unmarshaler)
  260. if !ok {
  261. // T doesn't work, try *T as well
  262. if v.Kind() != reflect.Ptr && v.CanAddr() {
  263. u, ok = v.Addr().Interface().(Unmarshaler)
  264. if ok {
  265. v = v.Addr()
  266. }
  267. }
  268. }
  269. if ok && (v.Kind() != reflect.Ptr || !v.IsNil()) {
  270. err := u.UnmarshalSexp(n)
  271. if err != nil {
  272. if ue, ok := err.(*UnmarshalError); ok {
  273. panic(ue)
  274. }
  275. n.unmarshal_error(v.Type(), err.Error())
  276. }
  277. return true
  278. }
  279. return false
  280. }
  281. func (n *Node) ensure_scalar(t reflect.Type) {
  282. if n.IsScalar() {
  283. return
  284. }
  285. n.unmarshal_error(t, "scalar value required")
  286. }
  287. func (n *Node) ensure_list(t reflect.Type) {
  288. if n.IsList() {
  289. return
  290. }
  291. n.unmarshal_error(t, "list value required")
  292. }
  293. func (n *Node) unmarshal_value(v reflect.Value, use_siblings bool) {
  294. t := v.Type()
  295. // we support one level of indirection at the moment
  296. if v.Kind() == reflect.Ptr {
  297. // if the pointer is nil, allocate a new element of the type it
  298. // points to
  299. if v.IsNil() {
  300. v.Set(reflect.New(t.Elem()))
  301. }
  302. v = v.Elem()
  303. }
  304. // try Unmarshaler interface
  305. if n.unmarshal_unmarshaler(v) {
  306. return
  307. }
  308. // fallback to default unmarshaling scheme
  309. switch v.Kind() {
  310. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  311. // TODO: more string -> int conversion options (hex, binary, octal, etc.)
  312. n.ensure_scalar(t)
  313. num, err := strconv.ParseInt(n.Value, 10, 64)
  314. if err != nil {
  315. n.unmarshal_error(t, err.Error())
  316. }
  317. if v.OverflowInt(num) {
  318. n.unmarshal_error(t, "integer overflow")
  319. }
  320. v.SetInt(num)
  321. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  322. // TODO: more string -> int conversion options (hex, binary, octal, etc.)
  323. n.ensure_scalar(t)
  324. num, err := strconv.ParseUint(n.Value, 10, 64)
  325. if err != nil {
  326. n.unmarshal_error(t, err.Error())
  327. }
  328. if v.OverflowUint(num) {
  329. n.unmarshal_error(t, "integer overflow")
  330. }
  331. v.SetUint(num)
  332. case reflect.Float32, reflect.Float64:
  333. n.ensure_scalar(t)
  334. num, err := strconv.ParseFloat(n.Value, 64)
  335. if err != nil {
  336. n.unmarshal_error(t, err.Error())
  337. }
  338. v.SetFloat(num)
  339. case reflect.Bool:
  340. n.ensure_scalar(t)
  341. switch n.Value {
  342. case "true":
  343. v.SetBool(true)
  344. case "false":
  345. v.SetBool(false)
  346. default:
  347. n.unmarshal_error(t, "undefined boolean value, use true|false")
  348. }
  349. case reflect.String:
  350. n.ensure_scalar(t)
  351. v.SetString(n.Value)
  352. case reflect.Array, reflect.Slice:
  353. if !use_siblings {
  354. n.ensure_list(t)
  355. }
  356. i := 0
  357. c := n.Children
  358. if use_siblings {
  359. c = n
  360. }
  361. for ; c != nil; c = c.Next {
  362. if i >= v.Len() {
  363. if v.Kind() == reflect.Array {
  364. break
  365. } else {
  366. v.Set(reflect.Append(v, reflect.Zero(t.Elem())))
  367. }
  368. }
  369. c.unmarshal_value(v.Index(i), false)
  370. i++
  371. }
  372. if i < v.Len() {
  373. if v.Kind() == reflect.Array {
  374. z := reflect.Zero(t.Elem())
  375. for n := v.Len(); i < n; i++ {
  376. v.Index(i).Set(z)
  377. }
  378. } else {
  379. v.SetLen(i)
  380. }
  381. }
  382. case reflect.Interface:
  383. if v.NumMethod() != 0 {
  384. n.unmarshal_error(t, "unsupported type")
  385. }
  386. v.Set(reflect.ValueOf(n.unmarshal_as_interface()))
  387. case reflect.Map:
  388. n.ensure_list(t)
  389. if v.IsNil() {
  390. v.Set(reflect.MakeMap(t))
  391. }
  392. keyv := reflect.New(t.Key()).Elem()
  393. valv := reflect.New(t.Elem()).Elem()
  394. err := n.IterKeyValues(func(key, val *Node) error {
  395. key.unmarshal_value(keyv, false)
  396. val.unmarshal_value(valv, false)
  397. v.SetMapIndex(keyv, valv)
  398. return nil
  399. })
  400. if err != nil {
  401. n.unmarshal_error(t, "%s", err)
  402. }
  403. case reflect.Struct:
  404. err := n.IterKeyValues(func(key, val *Node) error {
  405. var f reflect.StructField
  406. var ok bool
  407. var opts tag_options
  408. for i, n := 0, t.NumField(); i < n; i++ {
  409. var tagname string
  410. f = t.Field(i)
  411. tag := f.Tag.Get("sexp")
  412. if tag == "-" {
  413. continue
  414. }
  415. if f.Anonymous {
  416. continue
  417. }
  418. tagname, opts = parse_tag(tag)
  419. ok = tagname == key.Value
  420. if ok {
  421. break
  422. }
  423. ok = f.Name == key.Value
  424. if ok {
  425. break
  426. }
  427. ok = strings.EqualFold(f.Name, key.Value)
  428. if ok {
  429. break
  430. }
  431. }
  432. if ok {
  433. if f.PkgPath != "" {
  434. n.unmarshal_error(t, "writing to an unexported field")
  435. } else {
  436. v := v.FieldByIndex(f.Index)
  437. val.unmarshal_value(v, opts.contains("siblings"))
  438. }
  439. }
  440. return nil
  441. })
  442. if err != nil {
  443. n.unmarshal_error(t, "%s", err)
  444. }
  445. default:
  446. n.unmarshal_error(t, "unsupported type")
  447. }
  448. }
  449. func (n *Node) unmarshal_as_interface() interface{} {
  450. // interface parsing for sexp isn't really useful, the outcome is
  451. // []interface{} or string
  452. if n.IsList() {
  453. var s []interface{}
  454. for c := n.Children; c != nil; c = c.Next {
  455. s = append(s, c.unmarshal_as_interface())
  456. }
  457. return s
  458. }
  459. return n.Value
  460. }
  461. func (n *Node) unmarshal(v interface{}) (err error) {
  462. defer func() {
  463. if e := recover(); e != nil {
  464. if _, ok := e.(*UnmarshalError); ok {
  465. err = e.(error)
  466. } else {
  467. panic(e)
  468. }
  469. }
  470. }()
  471. pv := reflect.ValueOf(v)
  472. if pv.Kind() != reflect.Ptr || pv.IsNil() {
  473. panic("Node.Unmarshal expects a non-nil pointer argument")
  474. }
  475. n.unmarshal_value(pv.Elem(), false)
  476. return nil
  477. }