Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Functions | Variables
Main.cpp File Reference
#include "src/sksl/lex/DFA.h"
#include "src/sksl/lex/LexUtil.h"
#include "src/sksl/lex/NFA.h"
#include "src/sksl/lex/NFAtoDFA.h"
#include "src/sksl/lex/RegexNode.h"
#include "src/sksl/lex/RegexParser.h"
#include "src/sksl/lex/TransitionTable.h"
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>

Go to the source code of this file.

Functions

static void writeH (const DFA &dfa, const char *lexer, const char *token, const std::vector< std::string > &tokens, const char *hPath)
 
static void writeCPP (const DFA &dfa, const char *lexer, const char *token, const char *include, const char *cppPath)
 
static void process (const char *inPath, const char *lexer, const char *token, const char *hPath, const char *cppPath)
 
int main (int argc, const char **argv)
 

Variables

static constexpr const char HEADER []
 

Function Documentation

◆ main()

int main ( int  argc,
const char **  argv 
)

Definition at line 156 of file Main.cpp.

160 {
161 if (fOffset >= (int32_t)fText.length()) {
162 if (startOffset == (int32_t)fText.length() || kAccepts[state] == 255) {
163 return )" << token << "(" << token << R"(::Kind::TK_END_OF_FILE, startOffset, 0);
AtkStateType state

◆ process()

static void process ( const char *  inPath,
const char *  lexer,
const char *  token,
const char *  hPath,
const char *  cppPath 
)
static

Definition at line 114 of file Main.cpp.

115 : dfa.fTransitions) {
116 states = std::max(states, row.size());
117 }
118 out << "using State = " << (states <= 256 ? "uint8_t" : "uint16_t") << ";\n";
119
120 // Find the first character mapped in our DFA.
121 size_t startChar = 0;
122 for (; startChar < dfa.fCharMappings.size(); ++startChar) {
123 if (dfa.fCharMappings[startChar] != 0) {
124 break;
125 }
126 }
127
128 // Arbitrarily-chosen character which is greater than startChar, and should not appear in actual
129 // input.
130 SkASSERT(startChar < 18);
131 out << "static constexpr uint8_t kInvalidChar = 18;";
132 out << "static constexpr uint8_t kMappings[" << dfa.fCharMappings.size() - startChar << "] = {";
133 for (size_t index = startChar; index < dfa.fCharMappings.size(); ++index) {
134 out << std::to_string(dfa.fCharMappings[index]) << ", ";
135 }
136 out << "};\n";
137
138 WriteTransitionTable(out, dfa, states);
139
140 out << "static const uint8_t kAccepts[" << states << "] = {";
141 for (size_t i = 0; i < states; ++i) {
142 if (i < dfa.fAccepts.size() && dfa.fAccepts[i] != INVALID) {
143 out << " " << dfa.fAccepts[i] << ",";
144 } else {
145 out << " 255,";
146 }
147 }
148 out << "};\n";
149 out << "\n";
150
151 out << token << " " << lexer << "::next() {";
152 out << R"(
153 // Note that we cheat here: normally a lexer needs to worry about the case
154 // where a token has a prefix which is not itself a valid token - for instance,
#define INVALID
Definition LexUtil.h:13
#define SkASSERT(cond)
Definition SkAssert.h:116
void WriteTransitionTable(std::ofstream &out, const DFA &dfa, size_t states)

◆ writeCPP()

static void writeCPP ( const DFA dfa,
const char *  lexer,
const char *  token,
const char *  include,
const char *  cppPath 
)
static

Definition at line 63 of file Main.cpp.

64 : fKind(kind)
65 , fOffset(offset)
66 , fLength(length) {}
67
68 Kind fKind = Kind::TK_NONE;
69 int32_t fOffset = -1;
70 int32_t fLength = -1;
71};
72
73class )" << lexer << R"( {
74public:
75 void start(std::string_view text) {
76 fText = text;
77 fOffset = 0;
78 }
79
80 )" << token << R"( next();
81
82 struct Checkpoint {
83 int32_t fOffset;
84 };
85
86 Checkpoint getCheckpoint() const {
87 return {fOffset};
88 }
89
90 void rewindToCheckpoint(Checkpoint checkpoint) {
91 fOffset = checkpoint.fOffset;
92 }
93
94private:
95 std::string_view fText;
96 int32_t fOffset;
97};
98
99} // namespace
100#endif
101)";
102}
103
104static void writeCPP(const DFA& dfa, const char* lexer, const char* token, const char* include,
105 const char* cppPath) {
106 std::ofstream out(cppPath);
107 SkASSERT(out.good());
108 out << HEADER;
109 out << "#include \"" << include << "\"\n";
110 out << "\n";
111 out << "namespace SkSL {\n";
112 out << "\n";
static float next(float f)
size_t length
std::u16string text
Point offset

◆ writeH()

static void writeH ( const DFA dfa,
const char *  lexer,
const char *  token,
const std::vector< std::string > &  tokens,
const char *  hPath 
)
static

Definition at line 41 of file Main.cpp.

42 {
43 std::ofstream out(hPath);
44 SkASSERT(out.good());
45 out << HEADER;
46 out << "#ifndef SKSL_" << lexer << "\n";
47 out << "#define SKSL_" << lexer << "\n";
48 out << "#include <cstdint>\n";
49 out << "#include <string_view>\n";
50 out << "namespace SkSL {\n";
51 out << "\n";
52 out << "struct " << token << " {\n";
53 out << " enum class Kind {\n";
54 for (const std::string& t : tokens) {
55 out << " TK_" << t << ",\n";
56 }
57 out << " TK_NONE,";
58 out << R"(
59 };
60
61 )" << token << "() {}";
static constexpr const char HEADER[]
Definition Main.cpp:30

Variable Documentation

◆ HEADER

constexpr const char HEADER[]
staticconstexpr
Initial value:
=
"/*\n"
" * Copyright 2017 Google Inc.\n"
" *\n"
" * Use of this source code is governed by a BSD-style license that can be\n"
" * found in the LICENSE file.\n"
" */\n"
"/*****************************************************************************************\n"
" ******************** This file was generated by sksllex. Do not edit. *******************\n"
" *****************************************************************************************/\n"

Processes a .lex file and produces .h and .cpp files which implement a lexical analyzer. The .lex file is a text file with one token definition per line. Each line is of the form: <TOKEN_NAME> = <pattern> where <pattern> is either a regular expression (e.g [0-9]) or a double-quoted literal string.

Definition at line 30 of file Main.cpp.