1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 %skeleton "lalr1.cc" 17 %require "3.0.4" 18 %define api.namespace {OHOS} 19 %define parser_class_name {Parser} 20 %define api.token.constructor 21 %define api.value.type variant 22 %define api.token.prefix {TOKEN_} 23 %define parse.assert 24 %defines 25 %code requires 26 { 27 #include <iostream> 28 #include <string> 29 #include <vector> 30 #include <stdint.h> 31 #include <cmath> 32 using namespace std; 33 34 namespace OHOS { 35 class Scanner; 36 class Driver; 37 } 38 } 39 40 %code top 41 { 42 #include <iostream> 43 #include "scanner.h" 44 #include "parser.hpp" 45 #include "driver.h" 46 #include "location.hh" 47 yylex(OHOS::Scanner & scanner,OHOS::Driver & driver)48 static OHOS::Parser::symbol_type yylex(OHOS::Scanner& scanner,OHOS::Driver &driver) 49 { 50 return scanner.nextToken(); 51 } 52 using namespace OHOS; 53 } 54 55 %lex-param {OHOS::Scanner& scanner} 56 %lex-param {OHOS::Driver& driver} 57 %parse-param {OHOS::Scanner& scanner} 58 %parse-param {OHOS::Driver& driver} 59 60 %locations 61 62 %token END 0 63 64 /* import from lexer.l */ 65 %token<string> SELECTOR 66 %token<string> ATTRIBUTE 67 %token<string> VALUE 68 %token CHAR_L_BRACE /* ( */ 69 %token CHAR_R_BRACE /* ) */ 70 71 /* non-terminate-symbol */ 72 %start statements 73 %type<string> css_block css_block_begin css_block_end 74 %type<string> declare 75 76 %% 77 78 statements: css_block statements 79 { 80 } 81 82 statements: declare statements 83 { 84 } 85 86 statements: %empty 87 { 88 } 89 90 css_block: css_block_begin css_block_end 91 { 92 } 93 94 css_block_begin: SELECTOR CHAR_L_BRACE 95 { 96 std::string selector; 97 if ($1.substr(0, 1) == "#") { 98 selector = $1.substr(1); 99 } 100 if ($1.substr(0, 2) == "&:") { 101 selector = $1.substr(2); 102 } 103 driver.current->blocks[selector].parent = driver.current; 104 driver.current = &driver.current->blocks[selector]; 105 } 106 107 css_block_end: statements CHAR_R_BRACE 108 { 109 driver.current = driver.current->parent; 110 } 111 112 declare: ATTRIBUTE VALUE 113 { 114 $$ = $1 + $2; 115 std::string attribute = $1; 116 std::string value = $2; 117 118 // remove end : 119 attribute = attribute.substr(0, attribute.length() - 1); 120 121 // remove end ; 122 value = value.substr(0, value.length() - 1); 123 driver.current->declares[attribute] = value; 124 } 125 126 %% 127 128 void OHOS::Parser::error(const OHOS::location& location,const std::string& message) 129 { 130 for (int i = 0; i < 5; i++) { 131 fprintf(stderr, "wmlayout scss parse error: (%d,%d) - (%d,%d): %s\n", 132 location.begin.line, location.begin.column, 133 location.end.line, location.end.column, 134 message.c_str()); 135 } 136 } 137