variant_parser.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. package dbus
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "reflect"
  8. "strconv"
  9. "strings"
  10. "unicode/utf8"
  11. )
  12. type varParser struct {
  13. tokens []varToken
  14. i int
  15. }
  16. func (p *varParser) backup() {
  17. p.i--
  18. }
  19. func (p *varParser) next() varToken {
  20. if p.i < len(p.tokens) {
  21. t := p.tokens[p.i]
  22. p.i++
  23. return t
  24. }
  25. return varToken{typ: tokEOF}
  26. }
  27. type varNode interface {
  28. Infer() (Signature, error)
  29. String() string
  30. Sigs() sigSet
  31. Value(Signature) (interface{}, error)
  32. }
  33. func varMakeNode(p *varParser) (varNode, error) {
  34. var sig Signature
  35. for {
  36. t := p.next()
  37. switch t.typ {
  38. case tokEOF:
  39. return nil, io.ErrUnexpectedEOF
  40. case tokError:
  41. return nil, errors.New(t.val)
  42. case tokNumber:
  43. return varMakeNumNode(t, sig)
  44. case tokString:
  45. return varMakeStringNode(t, sig)
  46. case tokBool:
  47. if sig.str != "" && sig.str != "b" {
  48. return nil, varTypeError{t.val, sig}
  49. }
  50. b, err := strconv.ParseBool(t.val)
  51. if err != nil {
  52. return nil, err
  53. }
  54. return boolNode(b), nil
  55. case tokArrayStart:
  56. return varMakeArrayNode(p, sig)
  57. case tokVariantStart:
  58. return varMakeVariantNode(p, sig)
  59. case tokDictStart:
  60. return varMakeDictNode(p, sig)
  61. case tokType:
  62. if sig.str != "" {
  63. return nil, errors.New("unexpected type annotation")
  64. }
  65. if t.val[0] == '@' {
  66. sig.str = t.val[1:]
  67. } else {
  68. sig.str = varTypeMap[t.val]
  69. }
  70. case tokByteString:
  71. if sig.str != "" && sig.str != "ay" {
  72. return nil, varTypeError{t.val, sig}
  73. }
  74. b, err := varParseByteString(t.val)
  75. if err != nil {
  76. return nil, err
  77. }
  78. return byteStringNode(b), nil
  79. default:
  80. return nil, fmt.Errorf("unexpected %q", t.val)
  81. }
  82. }
  83. }
  84. type varTypeError struct {
  85. val string
  86. sig Signature
  87. }
  88. func (e varTypeError) Error() string {
  89. return fmt.Sprintf("dbus: can't parse %q as type %q", e.val, e.sig.str)
  90. }
  91. type sigSet map[Signature]bool
  92. func (s sigSet) Empty() bool {
  93. return len(s) == 0
  94. }
  95. func (s sigSet) Intersect(s2 sigSet) sigSet {
  96. r := make(sigSet)
  97. for k := range s {
  98. if s2[k] {
  99. r[k] = true
  100. }
  101. }
  102. return r
  103. }
  104. func (s sigSet) Single() (Signature, bool) {
  105. if len(s) == 1 {
  106. for k := range s {
  107. return k, true
  108. }
  109. }
  110. return Signature{}, false
  111. }
  112. func (s sigSet) ToArray() sigSet {
  113. r := make(sigSet, len(s))
  114. for k := range s {
  115. r[Signature{"a" + k.str}] = true
  116. }
  117. return r
  118. }
  119. type numNode struct {
  120. sig Signature
  121. str string
  122. val interface{}
  123. }
  124. var numSigSet = sigSet{
  125. Signature{"y"}: true,
  126. Signature{"n"}: true,
  127. Signature{"q"}: true,
  128. Signature{"i"}: true,
  129. Signature{"u"}: true,
  130. Signature{"x"}: true,
  131. Signature{"t"}: true,
  132. Signature{"d"}: true,
  133. }
  134. func (n numNode) Infer() (Signature, error) {
  135. if strings.ContainsAny(n.str, ".e") {
  136. return Signature{"d"}, nil
  137. }
  138. return Signature{"i"}, nil
  139. }
  140. func (n numNode) String() string {
  141. return n.str
  142. }
  143. func (n numNode) Sigs() sigSet {
  144. if n.sig.str != "" {
  145. return sigSet{n.sig: true}
  146. }
  147. if strings.ContainsAny(n.str, ".e") {
  148. return sigSet{Signature{"d"}: true}
  149. }
  150. return numSigSet
  151. }
  152. func (n numNode) Value(sig Signature) (interface{}, error) {
  153. if n.sig.str != "" && n.sig != sig {
  154. return nil, varTypeError{n.str, sig}
  155. }
  156. if n.val != nil {
  157. return n.val, nil
  158. }
  159. return varNumAs(n.str, sig)
  160. }
  161. func varMakeNumNode(tok varToken, sig Signature) (varNode, error) {
  162. if sig.str == "" {
  163. return numNode{str: tok.val}, nil
  164. }
  165. num, err := varNumAs(tok.val, sig)
  166. if err != nil {
  167. return nil, err
  168. }
  169. return numNode{sig: sig, val: num}, nil
  170. }
  171. func varNumAs(s string, sig Signature) (interface{}, error) {
  172. isUnsigned := false
  173. size := 32
  174. switch sig.str {
  175. case "n":
  176. size = 16
  177. case "i":
  178. case "x":
  179. size = 64
  180. case "y":
  181. size = 8
  182. isUnsigned = true
  183. case "q":
  184. size = 16
  185. isUnsigned = true
  186. case "u":
  187. isUnsigned = true
  188. case "t":
  189. size = 64
  190. isUnsigned = true
  191. case "d":
  192. d, err := strconv.ParseFloat(s, 64)
  193. if err != nil {
  194. return nil, err
  195. }
  196. return d, nil
  197. default:
  198. return nil, varTypeError{s, sig}
  199. }
  200. base := 10
  201. if strings.HasPrefix(s, "0x") {
  202. base = 16
  203. s = s[2:]
  204. }
  205. if strings.HasPrefix(s, "0") && len(s) != 1 {
  206. base = 8
  207. s = s[1:]
  208. }
  209. if isUnsigned {
  210. i, err := strconv.ParseUint(s, base, size)
  211. if err != nil {
  212. return nil, err
  213. }
  214. var v interface{} = i
  215. switch sig.str {
  216. case "y":
  217. v = byte(i)
  218. case "q":
  219. v = uint16(i)
  220. case "u":
  221. v = uint32(i)
  222. }
  223. return v, nil
  224. }
  225. i, err := strconv.ParseInt(s, base, size)
  226. if err != nil {
  227. return nil, err
  228. }
  229. var v interface{} = i
  230. switch sig.str {
  231. case "n":
  232. v = int16(i)
  233. case "i":
  234. v = int32(i)
  235. }
  236. return v, nil
  237. }
  238. type stringNode struct {
  239. sig Signature
  240. str string // parsed
  241. val interface{} // has correct type
  242. }
  243. var stringSigSet = sigSet{
  244. Signature{"s"}: true,
  245. Signature{"g"}: true,
  246. Signature{"o"}: true,
  247. }
  248. func (n stringNode) Infer() (Signature, error) {
  249. return Signature{"s"}, nil
  250. }
  251. func (n stringNode) String() string {
  252. return n.str
  253. }
  254. func (n stringNode) Sigs() sigSet {
  255. if n.sig.str != "" {
  256. return sigSet{n.sig: true}
  257. }
  258. return stringSigSet
  259. }
  260. func (n stringNode) Value(sig Signature) (interface{}, error) {
  261. if n.sig.str != "" && n.sig != sig {
  262. return nil, varTypeError{n.str, sig}
  263. }
  264. if n.val != nil {
  265. return n.val, nil
  266. }
  267. switch {
  268. case sig.str == "g":
  269. return Signature{n.str}, nil
  270. case sig.str == "o":
  271. return ObjectPath(n.str), nil
  272. case sig.str == "s":
  273. return n.str, nil
  274. default:
  275. return nil, varTypeError{n.str, sig}
  276. }
  277. }
  278. func varMakeStringNode(tok varToken, sig Signature) (varNode, error) {
  279. if sig.str != "" && sig.str != "s" && sig.str != "g" && sig.str != "o" {
  280. return nil, fmt.Errorf("invalid type %q for string", sig.str)
  281. }
  282. s, err := varParseString(tok.val)
  283. if err != nil {
  284. return nil, err
  285. }
  286. n := stringNode{str: s}
  287. if sig.str == "" {
  288. return stringNode{str: s}, nil
  289. }
  290. n.sig = sig
  291. switch sig.str {
  292. case "o":
  293. n.val = ObjectPath(s)
  294. case "g":
  295. n.val = Signature{s}
  296. case "s":
  297. n.val = s
  298. }
  299. return n, nil
  300. }
  301. func varParseString(s string) (string, error) {
  302. // quotes are guaranteed to be there
  303. s = s[1 : len(s)-1]
  304. buf := new(bytes.Buffer)
  305. for len(s) != 0 {
  306. r, size := utf8.DecodeRuneInString(s)
  307. if r == utf8.RuneError && size == 1 {
  308. return "", errors.New("invalid UTF-8")
  309. }
  310. s = s[size:]
  311. if r != '\\' {
  312. buf.WriteRune(r)
  313. continue
  314. }
  315. r, size = utf8.DecodeRuneInString(s)
  316. if r == utf8.RuneError && size == 1 {
  317. return "", errors.New("invalid UTF-8")
  318. }
  319. s = s[size:]
  320. switch r {
  321. case 'a':
  322. buf.WriteRune(0x7)
  323. case 'b':
  324. buf.WriteRune(0x8)
  325. case 'f':
  326. buf.WriteRune(0xc)
  327. case 'n':
  328. buf.WriteRune('\n')
  329. case 'r':
  330. buf.WriteRune('\r')
  331. case 't':
  332. buf.WriteRune('\t')
  333. case '\n':
  334. case 'u':
  335. if len(s) < 4 {
  336. return "", errors.New("short unicode escape")
  337. }
  338. r, err := strconv.ParseUint(s[:4], 16, 32)
  339. if err != nil {
  340. return "", err
  341. }
  342. buf.WriteRune(rune(r))
  343. s = s[4:]
  344. case 'U':
  345. if len(s) < 8 {
  346. return "", errors.New("short unicode escape")
  347. }
  348. r, err := strconv.ParseUint(s[:8], 16, 32)
  349. if err != nil {
  350. return "", err
  351. }
  352. buf.WriteRune(rune(r))
  353. s = s[8:]
  354. default:
  355. buf.WriteRune(r)
  356. }
  357. }
  358. return buf.String(), nil
  359. }
  360. var boolSigSet = sigSet{Signature{"b"}: true}
  361. type boolNode bool
  362. func (boolNode) Infer() (Signature, error) {
  363. return Signature{"b"}, nil
  364. }
  365. func (b boolNode) String() string {
  366. if b {
  367. return "true"
  368. }
  369. return "false"
  370. }
  371. func (boolNode) Sigs() sigSet {
  372. return boolSigSet
  373. }
  374. func (b boolNode) Value(sig Signature) (interface{}, error) {
  375. if sig.str != "b" {
  376. return nil, varTypeError{b.String(), sig}
  377. }
  378. return bool(b), nil
  379. }
  380. type arrayNode struct {
  381. set sigSet
  382. children []varNode
  383. val interface{}
  384. }
  385. func (n arrayNode) Infer() (Signature, error) {
  386. for _, v := range n.children {
  387. csig, err := varInfer(v)
  388. if err != nil {
  389. continue
  390. }
  391. return Signature{"a" + csig.str}, nil
  392. }
  393. return Signature{}, fmt.Errorf("can't infer type for %q", n.String())
  394. }
  395. func (n arrayNode) String() string {
  396. s := "["
  397. for i, v := range n.children {
  398. s += v.String()
  399. if i != len(n.children)-1 {
  400. s += ", "
  401. }
  402. }
  403. return s + "]"
  404. }
  405. func (n arrayNode) Sigs() sigSet {
  406. return n.set
  407. }
  408. func (n arrayNode) Value(sig Signature) (interface{}, error) {
  409. if n.set.Empty() {
  410. // no type information whatsoever, so this must be an empty slice
  411. return reflect.MakeSlice(typeFor(sig.str), 0, 0).Interface(), nil
  412. }
  413. if !n.set[sig] {
  414. return nil, varTypeError{n.String(), sig}
  415. }
  416. s := reflect.MakeSlice(typeFor(sig.str), len(n.children), len(n.children))
  417. for i, v := range n.children {
  418. rv, err := v.Value(Signature{sig.str[1:]})
  419. if err != nil {
  420. return nil, err
  421. }
  422. s.Index(i).Set(reflect.ValueOf(rv))
  423. }
  424. return s.Interface(), nil
  425. }
  426. func varMakeArrayNode(p *varParser, sig Signature) (varNode, error) {
  427. var n arrayNode
  428. if sig.str != "" {
  429. n.set = sigSet{sig: true}
  430. }
  431. if t := p.next(); t.typ == tokArrayEnd {
  432. return n, nil
  433. } else {
  434. p.backup()
  435. }
  436. Loop:
  437. for {
  438. t := p.next()
  439. switch t.typ {
  440. case tokEOF:
  441. return nil, io.ErrUnexpectedEOF
  442. case tokError:
  443. return nil, errors.New(t.val)
  444. }
  445. p.backup()
  446. cn, err := varMakeNode(p)
  447. if err != nil {
  448. return nil, err
  449. }
  450. if cset := cn.Sigs(); !cset.Empty() {
  451. if n.set.Empty() {
  452. n.set = cset.ToArray()
  453. } else {
  454. nset := cset.ToArray().Intersect(n.set)
  455. if nset.Empty() {
  456. return nil, fmt.Errorf("can't parse %q with given type information", cn.String())
  457. }
  458. n.set = nset
  459. }
  460. }
  461. n.children = append(n.children, cn)
  462. switch t := p.next(); t.typ {
  463. case tokEOF:
  464. return nil, io.ErrUnexpectedEOF
  465. case tokError:
  466. return nil, errors.New(t.val)
  467. case tokArrayEnd:
  468. break Loop
  469. case tokComma:
  470. continue
  471. default:
  472. return nil, fmt.Errorf("unexpected %q", t.val)
  473. }
  474. }
  475. return n, nil
  476. }
  477. type variantNode struct {
  478. n varNode
  479. }
  480. var variantSet = sigSet{
  481. Signature{"v"}: true,
  482. }
  483. func (variantNode) Infer() (Signature, error) {
  484. return Signature{"v"}, nil
  485. }
  486. func (n variantNode) String() string {
  487. return "<" + n.n.String() + ">"
  488. }
  489. func (variantNode) Sigs() sigSet {
  490. return variantSet
  491. }
  492. func (n variantNode) Value(sig Signature) (interface{}, error) {
  493. if sig.str != "v" {
  494. return nil, varTypeError{n.String(), sig}
  495. }
  496. sig, err := varInfer(n.n)
  497. if err != nil {
  498. return nil, err
  499. }
  500. v, err := n.n.Value(sig)
  501. if err != nil {
  502. return nil, err
  503. }
  504. return MakeVariant(v), nil
  505. }
  506. func varMakeVariantNode(p *varParser, sig Signature) (varNode, error) {
  507. n, err := varMakeNode(p)
  508. if err != nil {
  509. return nil, err
  510. }
  511. if t := p.next(); t.typ != tokVariantEnd {
  512. return nil, fmt.Errorf("unexpected %q", t.val)
  513. }
  514. vn := variantNode{n}
  515. if sig.str != "" && sig.str != "v" {
  516. return nil, varTypeError{vn.String(), sig}
  517. }
  518. return variantNode{n}, nil
  519. }
  520. type dictEntry struct {
  521. key, val varNode
  522. }
  523. type dictNode struct {
  524. kset, vset sigSet
  525. children []dictEntry
  526. val interface{}
  527. }
  528. func (n dictNode) Infer() (Signature, error) {
  529. for _, v := range n.children {
  530. ksig, err := varInfer(v.key)
  531. if err != nil {
  532. continue
  533. }
  534. vsig, err := varInfer(v.val)
  535. if err != nil {
  536. continue
  537. }
  538. return Signature{"a{" + ksig.str + vsig.str + "}"}, nil
  539. }
  540. return Signature{}, fmt.Errorf("can't infer type for %q", n.String())
  541. }
  542. func (n dictNode) String() string {
  543. s := "{"
  544. for i, v := range n.children {
  545. s += v.key.String() + ": " + v.val.String()
  546. if i != len(n.children)-1 {
  547. s += ", "
  548. }
  549. }
  550. return s + "}"
  551. }
  552. func (n dictNode) Sigs() sigSet {
  553. r := sigSet{}
  554. for k := range n.kset {
  555. for v := range n.vset {
  556. sig := "a{" + k.str + v.str + "}"
  557. r[Signature{sig}] = true
  558. }
  559. }
  560. return r
  561. }
  562. func (n dictNode) Value(sig Signature) (interface{}, error) {
  563. set := n.Sigs()
  564. if set.Empty() {
  565. // no type information -> empty dict
  566. return reflect.MakeMap(typeFor(sig.str)).Interface(), nil
  567. }
  568. if !set[sig] {
  569. return nil, varTypeError{n.String(), sig}
  570. }
  571. m := reflect.MakeMap(typeFor(sig.str))
  572. ksig := Signature{sig.str[2:3]}
  573. vsig := Signature{sig.str[3 : len(sig.str)-1]}
  574. for _, v := range n.children {
  575. kv, err := v.key.Value(ksig)
  576. if err != nil {
  577. return nil, err
  578. }
  579. vv, err := v.val.Value(vsig)
  580. if err != nil {
  581. return nil, err
  582. }
  583. m.SetMapIndex(reflect.ValueOf(kv), reflect.ValueOf(vv))
  584. }
  585. return m.Interface(), nil
  586. }
  587. func varMakeDictNode(p *varParser, sig Signature) (varNode, error) {
  588. var n dictNode
  589. if sig.str != "" {
  590. if len(sig.str) < 5 {
  591. return nil, fmt.Errorf("invalid signature %q for dict type", sig)
  592. }
  593. ksig := Signature{string(sig.str[2])}
  594. vsig := Signature{sig.str[3 : len(sig.str)-1]}
  595. n.kset = sigSet{ksig: true}
  596. n.vset = sigSet{vsig: true}
  597. }
  598. if t := p.next(); t.typ == tokDictEnd {
  599. return n, nil
  600. } else {
  601. p.backup()
  602. }
  603. Loop:
  604. for {
  605. t := p.next()
  606. switch t.typ {
  607. case tokEOF:
  608. return nil, io.ErrUnexpectedEOF
  609. case tokError:
  610. return nil, errors.New(t.val)
  611. }
  612. p.backup()
  613. kn, err := varMakeNode(p)
  614. if err != nil {
  615. return nil, err
  616. }
  617. if kset := kn.Sigs(); !kset.Empty() {
  618. if n.kset.Empty() {
  619. n.kset = kset
  620. } else {
  621. n.kset = kset.Intersect(n.kset)
  622. if n.kset.Empty() {
  623. return nil, fmt.Errorf("can't parse %q with given type information", kn.String())
  624. }
  625. }
  626. }
  627. t = p.next()
  628. switch t.typ {
  629. case tokEOF:
  630. return nil, io.ErrUnexpectedEOF
  631. case tokError:
  632. return nil, errors.New(t.val)
  633. case tokColon:
  634. default:
  635. return nil, fmt.Errorf("unexpected %q", t.val)
  636. }
  637. t = p.next()
  638. switch t.typ {
  639. case tokEOF:
  640. return nil, io.ErrUnexpectedEOF
  641. case tokError:
  642. return nil, errors.New(t.val)
  643. }
  644. p.backup()
  645. vn, err := varMakeNode(p)
  646. if err != nil {
  647. return nil, err
  648. }
  649. if vset := vn.Sigs(); !vset.Empty() {
  650. if n.vset.Empty() {
  651. n.vset = vset
  652. } else {
  653. n.vset = n.vset.Intersect(vset)
  654. if n.vset.Empty() {
  655. return nil, fmt.Errorf("can't parse %q with given type information", vn.String())
  656. }
  657. }
  658. }
  659. n.children = append(n.children, dictEntry{kn, vn})
  660. t = p.next()
  661. switch t.typ {
  662. case tokEOF:
  663. return nil, io.ErrUnexpectedEOF
  664. case tokError:
  665. return nil, errors.New(t.val)
  666. case tokDictEnd:
  667. break Loop
  668. case tokComma:
  669. continue
  670. default:
  671. return nil, fmt.Errorf("unexpected %q", t.val)
  672. }
  673. }
  674. return n, nil
  675. }
  676. type byteStringNode []byte
  677. var byteStringSet = sigSet{
  678. Signature{"ay"}: true,
  679. }
  680. func (byteStringNode) Infer() (Signature, error) {
  681. return Signature{"ay"}, nil
  682. }
  683. func (b byteStringNode) String() string {
  684. return string(b)
  685. }
  686. func (b byteStringNode) Sigs() sigSet {
  687. return byteStringSet
  688. }
  689. func (b byteStringNode) Value(sig Signature) (interface{}, error) {
  690. if sig.str != "ay" {
  691. return nil, varTypeError{b.String(), sig}
  692. }
  693. return []byte(b), nil
  694. }
  695. func varParseByteString(s string) ([]byte, error) {
  696. // quotes and b at start are guaranteed to be there
  697. b := make([]byte, 0, 1)
  698. s = s[2 : len(s)-1]
  699. for len(s) != 0 {
  700. c := s[0]
  701. s = s[1:]
  702. if c != '\\' {
  703. b = append(b, c)
  704. continue
  705. }
  706. c = s[0]
  707. s = s[1:]
  708. switch c {
  709. case 'a':
  710. b = append(b, 0x7)
  711. case 'b':
  712. b = append(b, 0x8)
  713. case 'f':
  714. b = append(b, 0xc)
  715. case 'n':
  716. b = append(b, '\n')
  717. case 'r':
  718. b = append(b, '\r')
  719. case 't':
  720. b = append(b, '\t')
  721. case 'x':
  722. if len(s) < 2 {
  723. return nil, errors.New("short escape")
  724. }
  725. n, err := strconv.ParseUint(s[:2], 16, 8)
  726. if err != nil {
  727. return nil, err
  728. }
  729. b = append(b, byte(n))
  730. s = s[2:]
  731. case '0':
  732. if len(s) < 3 {
  733. return nil, errors.New("short escape")
  734. }
  735. n, err := strconv.ParseUint(s[:3], 8, 8)
  736. if err != nil {
  737. return nil, err
  738. }
  739. b = append(b, byte(n))
  740. s = s[3:]
  741. default:
  742. b = append(b, c)
  743. }
  744. }
  745. return append(b, 0), nil
  746. }
  747. func varInfer(n varNode) (Signature, error) {
  748. if sig, ok := n.Sigs().Single(); ok {
  749. return sig, nil
  750. }
  751. return n.Infer()
  752. }