sourcemap.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package sourcemap // import "gopkg.in/sourcemap.v1"
  2. import (
  3. "io"
  4. "strings"
  5. "gopkg.in/sourcemap.v1/base64vlq"
  6. )
  7. type fn func(m *mappings) (fn, error)
  8. type sourceMap struct {
  9. Version int `json:"version"`
  10. File string `json:"file"`
  11. SourceRoot string `json:"sourceRoot"`
  12. Sources []string `json:"sources"`
  13. Names []interface{} `json:"names"`
  14. Mappings string `json:"mappings"`
  15. }
  16. type mapping struct {
  17. genLine int
  18. genCol int
  19. sourcesInd int
  20. sourceLine int
  21. sourceCol int
  22. namesInd int
  23. }
  24. type mappings struct {
  25. rd *strings.Reader
  26. dec *base64vlq.Decoder
  27. hasName bool
  28. value mapping
  29. values []mapping
  30. }
  31. func parseMappings(s string) ([]mapping, error) {
  32. rd := strings.NewReader(s)
  33. m := &mappings{
  34. rd: rd,
  35. dec: base64vlq.NewDecoder(rd),
  36. }
  37. m.value.genLine = 1
  38. m.value.sourceLine = 1
  39. err := m.parse()
  40. if err != nil {
  41. return nil, err
  42. }
  43. return m.values, nil
  44. }
  45. func (m *mappings) parse() error {
  46. next := parseGenCol
  47. for {
  48. c, err := m.rd.ReadByte()
  49. if err == io.EOF {
  50. m.pushValue()
  51. return nil
  52. }
  53. if err != nil {
  54. return err
  55. }
  56. switch c {
  57. case ',':
  58. m.pushValue()
  59. next = parseGenCol
  60. case ';':
  61. m.pushValue()
  62. m.value.genLine++
  63. m.value.genCol = 0
  64. next = parseGenCol
  65. default:
  66. err := m.rd.UnreadByte()
  67. if err != nil {
  68. return err
  69. }
  70. next, err = next(m)
  71. if err != nil {
  72. return err
  73. }
  74. }
  75. }
  76. }
  77. func parseGenCol(m *mappings) (fn, error) {
  78. n, err := m.dec.Decode()
  79. if err != nil {
  80. return nil, err
  81. }
  82. m.value.genCol += n
  83. return parseSourcesInd, nil
  84. }
  85. func parseSourcesInd(m *mappings) (fn, error) {
  86. n, err := m.dec.Decode()
  87. if err != nil {
  88. return nil, err
  89. }
  90. m.value.sourcesInd += n
  91. return parseSourceLine, nil
  92. }
  93. func parseSourceLine(m *mappings) (fn, error) {
  94. n, err := m.dec.Decode()
  95. if err != nil {
  96. return nil, err
  97. }
  98. m.value.sourceLine += n
  99. return parseSourceCol, nil
  100. }
  101. func parseSourceCol(m *mappings) (fn, error) {
  102. n, err := m.dec.Decode()
  103. if err != nil {
  104. return nil, err
  105. }
  106. m.value.sourceCol += n
  107. return parseNamesInd, nil
  108. }
  109. func parseNamesInd(m *mappings) (fn, error) {
  110. n, err := m.dec.Decode()
  111. if err != nil {
  112. return nil, err
  113. }
  114. m.hasName = true
  115. m.value.namesInd += n
  116. return parseGenCol, nil
  117. }
  118. func (m *mappings) pushValue() {
  119. if m.value.sourceLine == 1 && m.value.sourceCol == 0 {
  120. return
  121. }
  122. if m.hasName {
  123. m.values = append(m.values, m.value)
  124. m.hasName = false
  125. } else {
  126. m.values = append(m.values, mapping{
  127. genLine: m.value.genLine,
  128. genCol: m.value.genCol,
  129. sourcesInd: m.value.sourcesInd,
  130. sourceLine: m.value.sourceLine,
  131. sourceCol: m.value.sourceCol,
  132. namesInd: -1,
  133. })
  134. }
  135. }