2
2
EventedTokenizer ,
3
3
EntityParser ,
4
4
HTML5NamedCharRefs as namedCharRefs
5
- } from " simple-html-tokenizer" ;
6
- import { Program } from " ./types/nodes" ;
7
- import * as AST from " ./types/nodes" ;
5
+ } from ' simple-html-tokenizer' ;
6
+ import { Program } from ' ./types/nodes' ;
7
+ import * as AST from ' ./types/nodes' ;
8
8
import * as HandlebarsAST from './types/handlebars-ast' ;
9
9
import { Option } from '@glimmer/interfaces' ;
10
10
import { assert , expect } from '@glimmer/util' ;
@@ -33,46 +33,49 @@ export interface Attribute {
33
33
valueStartColumn : number ;
34
34
}
35
35
36
- export class Parser {
36
+ export abstract class Parser {
37
37
protected elementStack : Element [ ] = [ ] ;
38
38
private source : string [ ] ;
39
39
public currentAttribute : Option < Attribute > = null ;
40
- public currentNode : Option < AST . CommentStatement | AST . TextNode | Tag < 'StartTag' | 'EndTag' > > = null ;
40
+ public currentNode : Option <
41
+ AST . CommentStatement | AST . TextNode | Tag < 'StartTag' | 'EndTag' >
42
+ > = null ;
41
43
public tokenizer = new EventedTokenizer ( this , entityParser ) ;
42
44
43
45
constructor ( source : string ) {
44
- this . tokenizer . states . tagOpen = function ( this : EventedTokenizer ) {
45
- let char = this . consume ( ) ;
46
- if ( char === "!" ) {
47
- this [ 'state' ] = 'markupDeclaration' ;
48
- } else if ( char === "/" ) {
49
- this [ 'state' ] = 'endTagOpen' ;
50
- } else if ( / [ A - Z a - z ] / . test ( char ) ) {
51
- this [ 'state' ] = 'tagName' ;
52
- this [ 'delegate' ] . beginStartTag ( ) ;
53
- this [ 'delegate' ] . appendToTagName ( char ) ;
54
- }
55
- } ;
56
-
57
- this . tokenizer . states . endTagOpen = function ( this : EventedTokenizer ) {
58
- let char = this . consume ( ) ;
59
- if ( / [ A - Z a - z ] / . test ( char ) ) {
60
- this [ 'state' ] = 'tagName' ;
61
- this [ 'delegate' ] . beginEndTag ( ) ;
62
- this [ 'delegate' ] . appendToTagName ( char ) ;
63
- }
64
- } ;
65
-
66
46
this . source = source . split ( / (?: \r \n ? | \n ) / g) ;
67
47
}
68
48
49
+ abstract reset ( ) : void ;
50
+ abstract finishData ( ) : void ;
51
+ abstract tagOpen ( ) : void ;
52
+ abstract beginData ( ) : void ;
53
+ abstract appendToData ( char : string ) : void ;
54
+ abstract beginStartTag ( ) : void ;
55
+ abstract appendToTagName ( char : string ) : void ;
56
+ abstract beginAttribute ( ) : void ;
57
+ abstract appendToAttributeName ( char : string ) : void ;
58
+ abstract beginAttributeValue ( quoted : boolean ) : void ;
59
+ abstract appendToAttributeValue ( char : string ) : void ;
60
+ abstract finishAttributeValue ( ) : void ;
61
+ abstract markTagAsSelfClosing ( ) : void ;
62
+ abstract beginEndTag ( ) : void ;
63
+ abstract finishTag ( ) : void ;
64
+ abstract beginComment ( ) : void ;
65
+ abstract appendToCommentData ( char : string ) : void ;
66
+ abstract finishComment ( ) : void ;
67
+ abstract reportSyntaxError ( error : string ) : void ;
68
+
69
69
get currentAttr ( ) : Attribute {
70
70
return expect ( this . currentAttribute , 'expected attribute' ) ;
71
71
}
72
72
73
73
get currentTag ( ) : Tag < 'StartTag' | 'EndTag' > {
74
74
let node = this . currentNode ;
75
- assert ( node && ( node . type === 'StartTag' || node . type === 'EndTag' ) , 'expected tag' ) ;
75
+ assert (
76
+ node && ( node . type === 'StartTag' || node . type === 'EndTag' ) ,
77
+ 'expected tag'
78
+ ) ;
76
79
return node as Tag < 'StartTag' | 'EndTag' > ;
77
80
}
78
81
@@ -98,7 +101,6 @@ export class Parser {
98
101
let node = this . currentNode ;
99
102
assert ( node && node . type === 'TextNode' , 'expected a text node' ) ;
100
103
return node as AST . TextNode ;
101
-
102
104
}
103
105
104
106
acceptNode ( node : HandlebarsAST . Program ) : Program ;
@@ -111,7 +113,10 @@ export class Parser {
111
113
return this . elementStack [ this . elementStack . length - 1 ] ;
112
114
}
113
115
114
- sourceForNode ( node : HandlebarsAST . Node , endNode ?: { loc : HandlebarsAST . SourceLocation } ) : string {
116
+ sourceForNode (
117
+ node : HandlebarsAST . Node ,
118
+ endNode ?: { loc : HandlebarsAST . SourceLocation }
119
+ ) : string {
115
120
let firstLine = node . loc . start . line - 1 ;
116
121
let currentLine = firstLine - 1 ;
117
122
let firstColumn = node . loc . start . column ;
0 commit comments