1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at
6fb299fa2Sopenharmony_ci *
7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8fb299fa2Sopenharmony_ci *
9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and
13fb299fa2Sopenharmony_ci * limitations under the License.
14fb299fa2Sopenharmony_ci */
15fb299fa2Sopenharmony_ci
16fb299fa2Sopenharmony_ci%{
17fb299fa2Sopenharmony_ci#include <iostream>
18fb299fa2Sopenharmony_ci#include <cerrno>
19fb299fa2Sopenharmony_ci#include <climits>
20fb299fa2Sopenharmony_ci#include <cstdlib>
21fb299fa2Sopenharmony_ci#include <string>
22fb299fa2Sopenharmony_ci
23fb299fa2Sopenharmony_ci#include "parser.hpp"  // 包含由parser.y生成的头文件
24fb299fa2Sopenharmony_ci#include "scanner.h"   // 包含yyFlexLexer子类的头文件
25fb299fa2Sopenharmony_ci#include "location.hh" // 包含位置调试信息头文件
26fb299fa2Sopenharmony_ci
27fb299fa2Sopenharmony_ci/* 定义了YY_USER_ACTION,该宏在每个记号的语义动作之前被调用,来根据记号的长度设置位置的信息 */
28fb299fa2Sopenharmony_ci#define YY_USER_ACTION  loc.columns (yyleng);
29fb299fa2Sopenharmony_ci
30fb299fa2Sopenharmony_ciusing namespace Uscript;
31fb299fa2Sopenharmony_ci#define yyterminate() Parser::make_END(loc);
32fb299fa2Sopenharmony_ci%}
33fb299fa2Sopenharmony_ci
34fb299fa2Sopenharmony_ci/* 声明使用C++版本FLEXER */
35fb299fa2Sopenharmony_ci%option c++
36fb299fa2Sopenharmony_ci
37fb299fa2Sopenharmony_ci%option noyywrap
38fb299fa2Sopenharmony_ci
39fb299fa2Sopenharmony_ci/* 使用Scanner::yylex() */
40fb299fa2Sopenharmony_ci%option yyclass="Scanner"
41fb299fa2Sopenharmony_ci
42fb299fa2Sopenharmony_ci/* 一些与编译常量使用该前缀否则为yy */
43fb299fa2Sopenharmony_ci%option prefix="script_"
44fb299fa2Sopenharmony_ci
45fb299fa2Sopenharmony_ci%%
46fb299fa2Sopenharmony_ci%{
47fb299fa2Sopenharmony_ci  // C++ 兼容的词法分析器的规则,step函数把位置的起始值设置为与结束值相等
48fb299fa2Sopenharmony_ci  loc.step();
49fb299fa2Sopenharmony_ci%}
50fb299fa2Sopenharmony_ci
51fb299fa2Sopenharmony_ci"#".*    {
52fb299fa2Sopenharmony_ci            loc.step(); // 注释
53fb299fa2Sopenharmony_ci}
54fb299fa2Sopenharmony_ci
55fb299fa2Sopenharmony_ci"/*"([^\*]|(\*)*[^\*/])*(\*)*"*/" {
56fb299fa2Sopenharmony_ci            loc.step(); // 注释
57fb299fa2Sopenharmony_ci}
58fb299fa2Sopenharmony_ci
59fb299fa2Sopenharmony_ci"//".* |
60fb299fa2Sopenharmony_ci[ \t]     {
61fb299fa2Sopenharmony_ci            /* 跳过注释和空白符号
62fb299fa2Sopenharmony_ci             * step函数把位置的起始值设置为与结束值相等,这样位置就指向了上一个极少的结束位置。
63fb299fa2Sopenharmony_ci             * 由于注释和空白符号识别后并不会返回,而前一个step的调用是在上一次yylex返回时,所以此处需要手动更新记号的起始位置
64fb299fa2Sopenharmony_ci             */
65fb299fa2Sopenharmony_ci            loc.step();
66fb299fa2Sopenharmony_ci          }
67fb299fa2Sopenharmony_ci\n        {
68fb299fa2Sopenharmony_ci            loc.lines(yyleng); // 使用lines函数来更新位置信息中的行号
69fb299fa2Sopenharmony_ci            loc.step();
70fb299fa2Sopenharmony_ci          }
71fb299fa2Sopenharmony_ci
72fb299fa2Sopenharmony_ci"function"  { return Parser::make_FUNCTION(yytext, loc); }
73fb299fa2Sopenharmony_ci"for"       { return Parser::make_FOR(yytext, loc); }
74fb299fa2Sopenharmony_ci"while"     { return Parser::make_WHILE(yytext, loc); }
75fb299fa2Sopenharmony_ci"if"        { return Parser::make_IF(yytext, loc); } //return IF;
76fb299fa2Sopenharmony_ci"else"      { return Parser::make_ELSE(yytext, loc); } //return ELSE;
77fb299fa2Sopenharmony_ci"+"         { return Parser::make_ADD(yytext, loc); } //return ADD;
78fb299fa2Sopenharmony_ci"-"         { return Parser::make_SUB(yytext, loc); } //return SUB;
79fb299fa2Sopenharmony_ci"*"         { return Parser::make_MUL(yytext, loc); } //return MUL;
80fb299fa2Sopenharmony_ci"/"         { return Parser::make_DIV(yytext, loc); } //return DIV;
81fb299fa2Sopenharmony_ci"="         { return Parser::make_ASSIGN(yytext, loc); } //return ASSIGN;
82fb299fa2Sopenharmony_ci"=="        { return Parser::make_EQ(yytext, loc); } //return EQ;
83fb299fa2Sopenharmony_ci"&&"        { return Parser::make_AND(yytext, loc); } //return AND;
84fb299fa2Sopenharmony_ci"||"        { return Parser::make_OR(yytext, loc); } //return OR;
85fb299fa2Sopenharmony_ci"!="        { return Parser::make_NE(yytext, loc); } //return NE;
86fb299fa2Sopenharmony_ci">"         { return Parser::make_GT(yytext, loc); } //return GT;
87fb299fa2Sopenharmony_ci">="        { return Parser::make_GE(yytext, loc); } //return GE;
88fb299fa2Sopenharmony_ci"<"         { return Parser::make_LT(yytext, loc); } //return LT;
89fb299fa2Sopenharmony_ci"<="        { return Parser::make_LE(yytext, loc); } //return LE;
90fb299fa2Sopenharmony_ci"("         { return Parser::make_LP(yytext, loc); } //return LP;
91fb299fa2Sopenharmony_ci")"         { return Parser::make_RP(yytext, loc); } //return RP;
92fb299fa2Sopenharmony_ci"{"         { return Parser::make_LC(yytext, loc); } //return LC;
93fb299fa2Sopenharmony_ci"}"         { return Parser::make_RC(yytext, loc); } //return RC;
94fb299fa2Sopenharmony_ci";"         { return Parser::make_SEMICOLON(yytext, loc); } //return SEMICOLON;
95fb299fa2Sopenharmony_ci"break"     { return Parser::make_BREAK(yytext, loc); } //return BREAK;
96fb299fa2Sopenharmony_ci"continue"  { return Parser::make_CONTINUE(yytext, loc); } //return CONTINUE;
97fb299fa2Sopenharmony_ci"return"    { return Parser::make_RETURN(yytext, loc); } //return RETURN;
98fb299fa2Sopenharmony_ci","         { return Parser::make_COMMA(yytext, loc); } //return COMMA;
99fb299fa2Sopenharmony_ci
100fb299fa2Sopenharmony_ci[A-Za-z_][A-Za-z_0-9]* {
101fb299fa2Sopenharmony_ci    return Parser::make_IDENTIFIER(yytext, loc);
102fb299fa2Sopenharmony_ci}
103fb299fa2Sopenharmony_ci([1-9][0-9]*)|"0" {
104fb299fa2Sopenharmony_ci    return Parser::make_NUMBER(std::strtol(yytext, nullptr, 10),loc);
105fb299fa2Sopenharmony_ci}
106fb299fa2Sopenharmony_ci[0-9]+\.[0-9]+ {
107fb299fa2Sopenharmony_ci    char* end = nullptr;
108fb299fa2Sopenharmony_ci    return Parser::make_FLOAT(std::strtof(yytext, &end), loc);
109fb299fa2Sopenharmony_ci}
110fb299fa2Sopenharmony_ci
111fb299fa2Sopenharmony_ci\"[^\n"]+\" {
112fb299fa2Sopenharmony_ci    return Parser::make_STRING(std::string(yytext + 1, yyleng - 2), loc);
113fb299fa2Sopenharmony_ci}
114fb299fa2Sopenharmony_ci<<EOF>>   {
115fb299fa2Sopenharmony_ci    return yyterminate();
116fb299fa2Sopenharmony_ci}
117fb299fa2Sopenharmony_ci
118fb299fa2Sopenharmony_ci.   {
119fb299fa2Sopenharmony_ci    loc.step();
120fb299fa2Sopenharmony_ci}
121fb299fa2Sopenharmony_ci%%
122