Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 82 lines (73 sloc) 2.097 kB
1f4764d @DaveFet Adding Apache License 2.0
DaveFet authored
1 /**
2 * Copyright (c) 2008-2009 Facebook
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * See accompanying file LICENSE.txt.
17 *
18 * @author Marcel Laverdet
19 */
20
1407529 @laverdet Updated lexer \ parser definition
laverdet authored
21 #include "node.hpp"
22 #include "parser.hpp"
23 #ifdef DEBUG_BISON
24 extern int yydebug;
25 #endif
dced303 @laverdet Fix ambiguities between ObjectLiteral & Block
laverdet authored
26 using namespace std;
27 using namespace fbjs;
1407529 @laverdet Updated lexer \ parser definition
laverdet authored
28
620cf27 @laverdet Cleanup stuff
laverdet authored
29 void* fbjs_init_parser(fbjs_parse_extra* extra) {
1407529 @laverdet Updated lexer \ parser definition
laverdet authored
30
31 // Initialize the scanner.
32 void* scanner;
620cf27 @laverdet Cleanup stuff
laverdet authored
33 yylex_init_extra(extra, &scanner);
6ea3afb @laverdet Cleanup various deficiencies
laverdet authored
34 extra->error = NULL;
35 extra->error_line = 0;
36 extra->terminated = false;
620cf27 @laverdet Cleanup stuff
laverdet authored
37 extra->lineno = 1;
38 extra->last_tok = 0;
39 extra->last_paren_tok = 0;
1407529 @laverdet Updated lexer \ parser definition
laverdet authored
40
41 // Debug stuff
42 #ifdef DEBUG_BISON
43 yydebug = 1;
44 #endif
45 #ifdef DEBUG_FLEX
46 yyset_debug(1, scanner);
47 #endif
48
620cf27 @laverdet Cleanup stuff
laverdet authored
49 return scanner;
50 }
51
52 void fbjs_cleanup_parser(fbjs_parse_extra* extra, void* scanner) {
1407529 @laverdet Updated lexer \ parser definition
laverdet authored
53 yylex_destroy(scanner);
6ea3afb @laverdet Cleanup various deficiencies
laverdet authored
54 if (extra->error != NULL) {
55 string error(extra->error);
56 free(extra->error);
57 throw ParseException(error, extra->error_line);
1407529 @laverdet Updated lexer \ parser definition
laverdet authored
58 }
59 }
aec0c4a @laverdet Implement the ability to the parse a char*
laverdet authored
60
61 //
620cf27 @laverdet Cleanup stuff
laverdet authored
62 // Parse from a file
bd9ed38 @laverdet Support for typehints and object literal elisons
laverdet authored
63 NodeProgram::NodeProgram(FILE* file, node_parse_enum opts /* = PARSE_NONE */) : Node(1) {
620cf27 @laverdet Cleanup stuff
laverdet authored
64 fbjs_parse_extra extra;
65 void* scanner = fbjs_init_parser(&extra);
bd9ed38 @laverdet Support for typehints and object literal elisons
laverdet authored
66 extra.opts = opts;
620cf27 @laverdet Cleanup stuff
laverdet authored
67 yyrestart(file, scanner); // read from file
68 yyparse(scanner, this);
69 fbjs_cleanup_parser(&extra, scanner);
70 }
71
72 //
aec0c4a @laverdet Implement the ability to the parse a char*
laverdet authored
73 // Parser from a string
bd9ed38 @laverdet Support for typehints and object literal elisons
laverdet authored
74 NodeProgram::NodeProgram(const char* str, node_parse_enum opts /* = PARSE_NONE */) : Node(1) {
aec0c4a @laverdet Implement the ability to the parse a char*
laverdet authored
75 fbjs_parse_extra extra;
620cf27 @laverdet Cleanup stuff
laverdet authored
76 void* scanner = fbjs_init_parser(&extra);
bd9ed38 @laverdet Support for typehints and object literal elisons
laverdet authored
77 extra.opts = opts;
620cf27 @laverdet Cleanup stuff
laverdet authored
78 yy_scan_string(str, scanner); // read from string
aec0c4a @laverdet Implement the ability to the parse a char*
laverdet authored
79 yyparse(scanner, this);
620cf27 @laverdet Cleanup stuff
laverdet authored
80 fbjs_cleanup_parser(&extra, scanner);
aec0c4a @laverdet Implement the ability to the parse a char*
laverdet authored
81 }
Something went wrong with that request. Please try again.