|
| 1 | +import { locate } from 'locate-character'; |
| 2 | + |
| 3 | +const validNameChar = /[a-zA-Z0-9_$]/; |
| 4 | + |
| 5 | +export default function parse ( template ) { |
| 6 | + let i = 0; |
| 7 | + |
| 8 | + const root = { |
| 9 | + start: 0, |
| 10 | + end: template.length, |
| 11 | + type: 'Fragment', |
| 12 | + children: [] |
| 13 | + }; |
| 14 | + |
| 15 | + const stack = [ root ]; |
| 16 | + let current = root; |
| 17 | + |
| 18 | + function error ( message ) { |
| 19 | + const { line, column } = locate( template, i ); |
| 20 | + throw new Error( `${message} (${line}:${column})` ); |
| 21 | + } |
| 22 | + |
| 23 | + function match ( str ) { |
| 24 | + return template.slice( i, i + str.length ) === str; |
| 25 | + } |
| 26 | + |
| 27 | + function fragment () { |
| 28 | + const char = template[i]; |
| 29 | + |
| 30 | + while ( char === ' ' ) { |
| 31 | + i += 1; |
| 32 | + } |
| 33 | + |
| 34 | + if ( char === '<' ) { |
| 35 | + return tag; |
| 36 | + } |
| 37 | + |
| 38 | + if ( match( '{{' ) ) { |
| 39 | + return mustache; |
| 40 | + } |
| 41 | + |
| 42 | + return text; |
| 43 | + } |
| 44 | + |
| 45 | + function tag () { |
| 46 | + const start = i++; |
| 47 | + let char = template[ i ]; |
| 48 | + |
| 49 | + const isClosingTag = char === '/'; |
| 50 | + |
| 51 | + if ( isClosingTag ) { |
| 52 | + // this is a closing tag |
| 53 | + i += 1; |
| 54 | + char = template[ i ]; |
| 55 | + } |
| 56 | + |
| 57 | + // TODO handle cases like <li>one<li>two |
| 58 | + |
| 59 | + let name = ''; |
| 60 | + |
| 61 | + while ( validNameChar.test( char ) ) { |
| 62 | + name += char; |
| 63 | + i += 1; |
| 64 | + char = template[i]; |
| 65 | + } |
| 66 | + |
| 67 | + if ( isClosingTag ) { |
| 68 | + if ( char !== '>' ) error( `Expected '>'` ); |
| 69 | + |
| 70 | + i += 1; |
| 71 | + current.end = i; |
| 72 | + stack.pop(); |
| 73 | + current = stack[ stack.length - 1 ]; |
| 74 | + |
| 75 | + return fragment; |
| 76 | + } |
| 77 | + |
| 78 | + const element = { |
| 79 | + start, |
| 80 | + end: null, // filled in later |
| 81 | + type: 'Element', |
| 82 | + name, |
| 83 | + attributes: {}, |
| 84 | + children: [] |
| 85 | + }; |
| 86 | + |
| 87 | + current.children.push( element ); |
| 88 | + stack.push( element ); |
| 89 | + |
| 90 | + current = element; |
| 91 | + |
| 92 | + if ( char === '>' ) { |
| 93 | + i += 1; |
| 94 | + return fragment; |
| 95 | + } |
| 96 | + |
| 97 | + return attributes; |
| 98 | + } |
| 99 | + |
| 100 | + function text () { |
| 101 | + const start = i; |
| 102 | + |
| 103 | + let data = ''; |
| 104 | + |
| 105 | + while ( i < template.length && template[i] !== '<' && !match( '{{' ) ) { |
| 106 | + data += template[ i++ ]; |
| 107 | + } |
| 108 | + |
| 109 | + current.children.push({ |
| 110 | + start, |
| 111 | + end: i, |
| 112 | + type: 'Text', |
| 113 | + data |
| 114 | + }); |
| 115 | + |
| 116 | + return fragment; |
| 117 | + } |
| 118 | + |
| 119 | + function attributes () { |
| 120 | + const char = template[i]; |
| 121 | + if ( char === '>' ) { |
| 122 | + i += 1; |
| 123 | + return fragment; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + let state = fragment; |
| 128 | + |
| 129 | + while ( i < template.length ) { |
| 130 | + state = state(); |
| 131 | + } |
| 132 | + |
| 133 | + return root; |
| 134 | +} |
0 commit comments