This commit is contained in:
anon 2023-11-20 23:40:47 +01:00
parent 8ff70bb801
commit 941199e354
7 changed files with 91 additions and 36 deletions

View File

@ -8,14 +8,6 @@
#include "exit_values.hpp"
#include "scanner.hpp"
#define DECLARE_LEXER(x) \
extern FILE * x ## _in; \
extern FILE * x ## _out; \
extern int x ## _lex(void); \
DECLARE_LEXER(csml);
DECLARE_LEXER(xml);
extern std::stack<std::string> csml_tag_stack;
extern unsigned xml_tag_stack;

View File

@ -21,6 +21,7 @@ void _ECHO_CANDIDATE(){
}
#define ECHO_CANDIDATE _ECHO_CANDIDATE()
#define FLUSH_CANDIDATE do { _ECHO_CANDIDATE(); tag_candidate = ""; } while (0)
static const char COMMENT_START[] = "<!--";
static const char COMMENT_END[] = "-->";
@ -61,16 +62,17 @@ void pop_tag() {
%x BODY HEAD HEAD_VALUE
%x COMMENT COMMENT_MULTILINE
%x IGNORE IGNORE_COUNT_START IGNORE_COUNT_END
%x TAG_ASYMETRIC_SPECIAL
%s DECLARATION
%s UNICODE
ws [ \t\r\v\f]
wsnl [ \t\r\v\f\n]
nwsnl [^ \t\r\v\f\n]
identifier [A-z][A-z0-9]*
unicode [\300-\364]
%%
BEGIN BODY;
<BODY>{
@ -92,8 +94,7 @@ unicode [\300-\364]
BEGIN HEAD;
}
&#?{identifier}; {
ECHO_CANDIDATE;
tag_candidate = "";
FLUSH_CANDIDATE;
ECHO;
}
; {
@ -114,14 +115,12 @@ unicode [\300-\364]
pop_tag();
}
\#\! {
ECHO_CANDIDATE;
FLUSH_CANDIDATE;
ECHOS("<!");
tag_candidate = "";
BEGIN DECLARATION;
}
{unicode} {
ECHO_CANDIDATE;
tag_candidate = "";
FLUSH_CANDIDATE;
const char mask = (char)0b100000000;
const char &header = yytext[0];
@ -132,17 +131,36 @@ unicode [\300-\364]
yyless(0);
BEGIN UNICODE;
}
\<{nwsnl}* {
{ // XXX: this is way too expensive
const auto filter = [yyleng, yytext](std::string e){
return e == std::string(yytext+1).substr(0, e.size());
};
const auto search_result = std::find_if(asymmetric_special_list.begin(),
asymmetric_special_list.end(),
filter
);
is_asymmetric = search_result != asymmetric_special_list.end();
if (is_asymmetric) {
ECHO;
BEGIN TAG_ASYMETRIC_SPECIAL;
} else {
FLUSH_CANDIDATE;
ECHOS("&lt;");
}
}
}
\< {
ECHO_CANDIDATE;
tag_candidate = "";
FLUSH_CANDIDATE;
ECHOS("&lt;");
}
\> {
ECHO_CANDIDATE;
tag_candidate = "";
FLUSH_CANDIDATE;
ECHOS("&gt;");
}
.|{wsnl} {
FLUSH_CANDIDATE;
ECHO;
}
}
@ -219,7 +237,7 @@ unicode [\300-\364]
<IGNORE_COUNT_END>{
\} {
if (++ignore_i >= ignore_count) {
ignore_i = 0;
ignore_i = 1;
ignore_count = 1;
ECHOS(buffer.c_str());
@ -257,6 +275,22 @@ unicode [\300-\364]
}
}
<TAG_ASYMETRIC_SPECIAL>{
.\> {
ECHO;
is_asymmetric = std::find(asymmetric_special_list.begin(),
asymmetric_special_list.end(),
(std::string("") + yytext[0]))
!= asymmetric_special_list.end();
if (is_asymmetric) {
BEGIN BODY;
}
}
.|\n {
ECHO;
}
}
<DECLARATION>{
[^\\]; {
ECHOC(yytext[0]);

View File

@ -13,11 +13,16 @@
std::vector<std::string> ignore_list;
std::vector<std::string> asymmetric_special_list;
bool is_asymmetric;
int ignore_count = 1;
int ignore_i = 1;
std::string buffer;
extern int xml_lex_destroy(void);
extern int csml_lex_destroy(void);
char * output_name_from_input_name(const char * const input, const char * const extension) {
char * input_duplicate = strdup(input);
char * dn = strdup(dirname(input_duplicate));
@ -77,6 +82,9 @@ signed main(int argc, char * * argv) {
}
parse_round2_arguments(argc - 1, argv + 1);
xml_lex_destroy();
csml_lex_destroy();
return EXIT_SUCCESS;
}

View File

@ -4,6 +4,15 @@
#include <string>
#include <algorithm>
#define DECLARE_LEXER(x) \
extern FILE * x ## _in; \
extern FILE * x ## _out; \
extern int x ## _lex(void); \
extern int x ## _lex_destroy(void);
DECLARE_LEXER(csml);
DECLARE_LEXER(xml);
#define ECHOS(s) do { \
const char * const ss = s; \
fwrite(ss, strlen(ss), sizeof(char), yyout); \
@ -14,6 +23,8 @@
extern std::vector<std::string> ignore_list;
extern std::vector<std::string> asymmetric_special_list;
extern bool is_asymmetric;
inline
bool do_ignore(const std::string &current_tag) {
return std::find(ignore_list.begin(),

View File

@ -7,11 +7,11 @@
#include "html_special.hpp"
#include "exit_values.hpp"
bool is_comment_multiline;
std::string current_tag;
unsigned xml_tag_stack = 0;
bool is_asymmetric;
char current_string_quote;
static bool is_comment_multiline;
static std::string current_tag;
static char current_string_quote;
int state_buffer;
@ -210,7 +210,16 @@ identifier [A-z][A-z0-9]*
<STRING>{
[^\\](\"|\') {
if (current_string_quote == yytext[1]) {
ECHOC(yytext[0]);
{
// XXX: this is such a hack
switch (yytext[0]) {
case ',':
case '(':
case ')':
ECHOC('\\');
}
ECHOC(yytext[0]);
}
if (state_buffer == DECLARATION) {
ECHOC(yytext[1]);
}
@ -219,8 +228,9 @@ identifier [A-z][A-z0-9]*
ECHO;
}
}
, {
ECHOS("\\,");
[,)(] {
ECHOC('\\');
ECHOC(yytext[0]);
}
.|\n {
ECHO;

View File

@ -57,13 +57,13 @@ body {
div (id: container) {
h1 {<?php echo "Hello, PHP!"; ?>}
p (id: output) {Welcome to the complex world of programming.}
button (onclick: changeOutput()) {Click me!}
button (onclick: changeOutput\(\)) {Click me!}
}
script {{
function changeOutput() {
document.getElementById("output").innerHTML = "You clicked the button! The world just got more complex.";
}
}}
}
}

View File

@ -1,9 +1,9 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<html lang='en'>
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta charset='UTF-8'/>
<meta name='viewport' content='width=device-width, initial-scale=1.0'/>
<title>Complex Hello, World!</title>
<style>
body {
@ -54,16 +54,16 @@
</style>
</head>
<body>
<div id="container">
<div id='container'>
<h1><?php echo "Hello, PHP!"; ?></h1>
<p id="output">Welcome to the complex world of programming.</p>
<button onclick="changeOutput()">Click me!</button>
<p id='output'>Welcome to the complex world of programming.</p>
<button onclick='changeOutput()'>Click me!</button>
</div>
<script>
function changeOutput() {
document.getElementById("output").innerHTML = "You clicked the button! The world just got more complex.";
}
</script>
</body>
</html>