remove String module; use std::string
This commit is contained in:
parent
accaed47bc
commit
24f18c5233
@ -1,9 +0,0 @@
|
|||||||
#include "Node.h"
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
Node * Node_new(int type)
|
|
||||||
{
|
|
||||||
Node * node = (Node *)malloc(sizeof(Node));
|
|
||||||
node->type = type;
|
|
||||||
return node;
|
|
||||||
}
|
|
2
src/Node.cc
Normal file
2
src/Node.cc
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
#include "Node.h"
|
||||||
|
#include <stdlib.h>
|
26
src/Node.h
26
src/Node.h
@ -1,35 +1,31 @@
|
|||||||
#ifndef NODE_H
|
#ifndef NODE_H
|
||||||
#define NODE_H
|
#define NODE_H
|
||||||
|
|
||||||
#include "String.h"
|
#include <string>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
class Node
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
Node(int _type)
|
||||||
|
{
|
||||||
|
type = _type;
|
||||||
|
}
|
||||||
|
|
||||||
int type;
|
int type;
|
||||||
union
|
union
|
||||||
{
|
{
|
||||||
struct
|
struct
|
||||||
{
|
{
|
||||||
String * fname;
|
std::string * fname;
|
||||||
size_t line;
|
size_t line;
|
||||||
String * text;
|
std::string * text;
|
||||||
} token;
|
} token;
|
||||||
};
|
};
|
||||||
} Node;
|
};
|
||||||
|
|
||||||
enum
|
enum
|
||||||
{
|
{
|
||||||
NODE_TYPE_TOKEN,
|
NODE_TYPE_TOKEN,
|
||||||
};
|
};
|
||||||
|
|
||||||
Node * Node_new(int type);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
53
src/String.c
53
src/String.c
@ -1,53 +0,0 @@
|
|||||||
#include "String.h"
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
String * String_new(const char * s)
|
|
||||||
{
|
|
||||||
size_t size = strlen(s);
|
|
||||||
return String_new_size(s, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
String * String_new_size(const char * s, size_t size)
|
|
||||||
{
|
|
||||||
String * new_st = (String *)malloc(sizeof(String));
|
|
||||||
char * smem = (char *)malloc(size + 1u);
|
|
||||||
memcpy(smem, s, size);
|
|
||||||
smem[size] = '\0';
|
|
||||||
new_st->value = smem;
|
|
||||||
new_st->size = size;
|
|
||||||
return new_st;
|
|
||||||
}
|
|
||||||
|
|
||||||
String * String_plus(const String * st, const char * s)
|
|
||||||
{
|
|
||||||
String * new_st = (String *)malloc(sizeof(String));
|
|
||||||
size_t size1 = st->size;
|
|
||||||
size_t size2 = strlen(s);
|
|
||||||
size_t size = size1 + size2;
|
|
||||||
char * smem = (char *)malloc(size + 1u);
|
|
||||||
memcpy(smem, st->value, size1);
|
|
||||||
memcpy(&smem[size1], s, size2);
|
|
||||||
smem[size] = '\0';
|
|
||||||
new_st->value = smem;
|
|
||||||
new_st->size = size;
|
|
||||||
return new_st;
|
|
||||||
}
|
|
||||||
|
|
||||||
String * String_concat(String * st, const char * s)
|
|
||||||
{
|
|
||||||
size_t size2 = strlen(s);
|
|
||||||
size_t size = st->size + size2;
|
|
||||||
char * smem = (char *)malloc(size + 1u);
|
|
||||||
memcpy(smem, st->value, st->size);
|
|
||||||
memcpy(&smem[st->size], s, size2);
|
|
||||||
smem[size] = '\0';
|
|
||||||
st->value = smem;
|
|
||||||
st->size = size;
|
|
||||||
return st;
|
|
||||||
}
|
|
||||||
|
|
||||||
void String_free(String * st)
|
|
||||||
{
|
|
||||||
free(st->value);
|
|
||||||
free(st);
|
|
||||||
}
|
|
28
src/String.h
28
src/String.h
@ -1,28 +0,0 @@
|
|||||||
#ifndef STRING_H
|
|
||||||
#define STRING_H
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
char * value;
|
|
||||||
size_t size;
|
|
||||||
} String;
|
|
||||||
|
|
||||||
String * String_new(const char * s);
|
|
||||||
String * String_new_size(const char * s, size_t size);
|
|
||||||
String * String_plus(const String * st, const char * s);
|
|
||||||
String * String_concat(String * st, const char * s);
|
|
||||||
static inline char * String_cstr(const String * st) { return st->value; }
|
|
||||||
static inline size_t String_size(const String * st) { return st->size; }
|
|
||||||
void String_free(String * st);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
@ -4,23 +4,23 @@
|
|||||||
%{
|
%{
|
||||||
|
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
#include "String.h"
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "Node.h"
|
#include "Node.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#define YY_USER_ACTION \
|
#define YY_USER_ACTION \
|
||||||
do { \
|
do { \
|
||||||
yylloc->first_column += yyleng; \
|
yylloc->first_column += yyleng; \
|
||||||
yylloc->last_column += yyleng; \
|
yylloc->last_column += yyleng; \
|
||||||
*yylval = Node_new(NODE_TYPE_TOKEN); \
|
*yylval = new Node(NODE_TYPE_TOKEN); \
|
||||||
(*yylval)->token.fname = current_file; \
|
(*yylval)->token.fname = current_file; \
|
||||||
(*yylval)->token.line = current_line; \
|
(*yylval)->token.line = current_line; \
|
||||||
(*yylval)->token.text = String_new(yytext); \
|
(*yylval)->token.text = new std::string(yytext); \
|
||||||
} while(0);
|
} while(0);
|
||||||
|
|
||||||
static String * build_string = NULL;
|
static std::string * build_string = nullptr;
|
||||||
static size_t current_line = 1u;
|
static size_t current_line = 1u;
|
||||||
static String * current_file = NULL;
|
static std::string * current_file = nullptr;
|
||||||
void handle_loc(const char * input);
|
void handle_loc(const char * input);
|
||||||
|
|
||||||
%}
|
%}
|
||||||
@ -138,11 +138,11 @@ L?'\\[0-7]{1,3}' return CHAR_CONST;
|
|||||||
([0-9]+\.[0-9]*|\.[0-9]+)([eE][-+]?[0-9]+)?[fFlL]? return FLOAT_CONST;
|
([0-9]+\.[0-9]*|\.[0-9]+)([eE][-+]?[0-9]+)?[fFlL]? return FLOAT_CONST;
|
||||||
|
|
||||||
L?\" {
|
L?\" {
|
||||||
if (build_string != NULL)
|
if (build_string != nullptr)
|
||||||
{
|
{
|
||||||
String_free(build_string);
|
delete build_string;
|
||||||
}
|
}
|
||||||
build_string = String_new("");
|
build_string = new std::string();
|
||||||
BEGIN(str);
|
BEGIN(str);
|
||||||
}
|
}
|
||||||
<str>{
|
<str>{
|
||||||
@ -154,25 +154,23 @@ L?\" {
|
|||||||
/* hexadecimal escape code */
|
/* hexadecimal escape code */
|
||||||
unsigned int val;
|
unsigned int val;
|
||||||
(void)sscanf(yytext + 1, "%x", &val);
|
(void)sscanf(yytext + 1, "%x", &val);
|
||||||
char v[2] = {(char)val, '\0'};
|
*build_string += (char)val;
|
||||||
String_concat(build_string, v);
|
|
||||||
}
|
}
|
||||||
\\[0-7]{1,3} {
|
\\[0-7]{1,3} {
|
||||||
/* octal escape code */
|
/* octal escape code */
|
||||||
unsigned int val;
|
unsigned int val;
|
||||||
(void)sscanf(yytext + 1, "%o", &val);
|
(void)sscanf(yytext + 1, "%o", &val);
|
||||||
char v[2] = {(char)val, '\0'};
|
*build_string += (char)val;
|
||||||
String_concat(build_string, v);
|
|
||||||
}
|
}
|
||||||
\\a String_concat(build_string, "\a");
|
\\a *build_string += '\a';
|
||||||
\\b String_concat(build_string, "\b");
|
\\b *build_string += '\b';
|
||||||
\\f String_concat(build_string, "\f");
|
\\f *build_string += '\f';
|
||||||
\\n String_concat(build_string, "\n");
|
\\n *build_string += '\n';
|
||||||
\\r String_concat(build_string, "\r");
|
\\r *build_string += '\r';
|
||||||
\\t String_concat(build_string, "\t");
|
\\t *build_string += '\t';
|
||||||
\\v String_concat(build_string, "\v");
|
\\v *build_string += '\v';
|
||||||
\\. String_concat(build_string, &yytext[1]);
|
\\. *build_string += (char)yytext[1];
|
||||||
[^\\\"]+ String_concat(build_string, yytext);
|
[^\\\"]+ *build_string += yytext;
|
||||||
}
|
}
|
||||||
|
|
||||||
[a-zA-Z_][a-zA-Z_0-9]* return IDENTIFIER;
|
[a-zA-Z_][a-zA-Z_0-9]* return IDENTIFIER;
|
||||||
@ -209,5 +207,5 @@ void handle_loc(const char * input)
|
|||||||
input++;
|
input++;
|
||||||
}
|
}
|
||||||
size_t fname_len = (input - fname_start);
|
size_t fname_len = (input - fname_start);
|
||||||
current_file = String_new_size(fname_start, fname_len);
|
current_file = new std::string(fname_start, fname_len);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user