Compute constructs a location mapping from input to output. An error is reported if any of the tokens of output cannot be mapped.
(input, output []byte)
| 42 | // Compute constructs a location mapping from input to output. An error is |
| 43 | // reported if any of the tokens of output cannot be mapped. |
| 44 | func Compute(input, output []byte) (Map, error) { |
| 45 | itok := tokenize(input) |
| 46 | otok := tokenize(output) |
| 47 | if len(itok) != len(otok) { |
| 48 | return nil, fmt.Errorf("wrong number of tokens, %d ≠ %d", len(itok), len(otok)) |
| 49 | } |
| 50 | m := make(Map) |
| 51 | for i, ti := range itok { |
| 52 | to := otok[i] |
| 53 | if ti.Token != to.Token { |
| 54 | return nil, fmt.Errorf("token %d type mismatch: %s ≠ %s", i+1, ti, to) |
| 55 | } |
| 56 | m.add(ti.pos, ti.end, to.pos, to.end) |
| 57 | } |
| 58 | return m, nil |
| 59 | } |
| 60 | |
| 61 | // tokinfo records the span and type of a source token. |
| 62 | type tokinfo struct { |