Skip to content

Commit

Permalink
string
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-johnson-4 committed Feb 8, 2025
1 parent 4ff7702 commit 286346c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
20 changes: 10 additions & 10 deletions PLUGINS/FRONTEND/C/c-parse.lsts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
type CConstant = CConstantInteger{value:String}
| CConstantCharacter{value:String}
| CConstantFloating{value:String}
| CConstantEnumeration{value:String};
| CConstantEnumeration{value:String}
| CConstantString{value:String};

let cmp(l: CConstant, r: CConstant): Ord = (
if $".0"(l) != $".0"(r) then cmp($".0"(l), $".0"(r))
Expand All @@ -18,6 +19,7 @@ let cmp(l: CConstant, r: CConstant): Ord = (
Tuple{ first:CConstantCharacter{lv=value}, second:CConstantCharacter{rv=value} } => cmp(lv, rv);
Tuple{ first:CConstantFloating{lv=value}, second:CConstantFloating{rv=value} } => cmp(lv, rv);
Tuple{ first:CConstantEnumeration{lv=value}, second:CConstantEnumeration{rv=value} } => cmp(lv, rv);
Tuple{ first:CConstantString{lv=value}, second:CConstantString{rv=value} } => cmp(lv, rv);
}
);

Expand Down Expand Up @@ -85,6 +87,7 @@ let std-c-has-class(tks: String, cls: String): U64 = (
"character" => tk == r/^(u8|u|U|L)?[']([^']|([\\][']))+[']/; # character constant
"floating" => tk == r/^[0-9]+([.][0-9]+)?([eE][0-9]+)?[fF]?/ # decimal constant
|| tk == r/^[0][x][0-9a-fA-F]+([.][0-9a-fA-F]+)?([eE][0-9a-fA-F]+)?([pP][0-9]+)[fF]?/; # hexadecimal constant
"string" => tk == r/^[RLuU8]*["]([^"\\]|([\\].))*["]/;
"enumeration" => std-c-enumeration-constant-index.has(tks);
_ => tk == cls;
}
Expand Down Expand Up @@ -419,8 +422,6 @@ let std-c-parse-identifier-list(tokens: List<Token>): Tuple<Maybe<List<String>>,
#(* NOTE: Please define enumeration-constant for identifier inside enum { ... }. *)
#enumerator = enumeration-constant, ['=', constant-expression];

#enumeration-constant = identifier;

#type-name = specifier-qualifier-list, [abstract-declarator];

#specifier-qualifier-list = specifier-qualifier, {specifier-qualifier};
Expand Down Expand Up @@ -507,11 +508,12 @@ let std-c-parse-constant(tokens: List<Token>): Tuple<Maybe<CConstant>,List<Token
else Tuple{ no, tokens }
);

#constant = integer-constant
# | character-constant
# | floating-constant
# | enumeration-constant;

let std-c-parse-string(tokens: List<Token>): Tuple<Maybe<CConstant>,List<Token>> = (
let no = None :: Maybe<CConstant>;
if std-c-can-take(tokens, "string") then Tuple{ Some{CConstantString{head(tokens).skey}}, tail(tokens) }
else if std-c-can-take(tokens, "__func__") then Tuple{ Some{CConstantString{head(tokens).skey}}, tail(tokens) }
else Tuple{ no, tokens }
);

#parameter-list = parameter-declaration, {',', parameter-declaration};

Expand Down Expand Up @@ -571,8 +573,6 @@ let std-c-parse-constant(tokens: List<Token>): Tuple<Maybe<CConstant>,List<Token
#argument-expression-list = assignment-expression, {',', assignment-expression};


#string = string-literal
# | '__func__';

#generic-selection = '_Generic', '(', assignment-expression, ',', generic-assoc-list, ')';

Expand Down
8 changes: 4 additions & 4 deletions PLUGINS/FRONTEND/C/c-smart-tokenize.lsts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ let std-c-tokenize-string(file-path: String, text: String): List<Token> = (
tokens = cons(text[:cl.length], tokens); text = rest;
);

(id=r/^[a-zA-Z0-9_]+/).. rest => (
tokens = cons(text[:id.length], tokens); text = rest;
(lit=r/^[RLuU8]*["]([^"\\]|([\\].))*["]/).. rest => (
tokens = cons(text[:lit.length], tokens); text = rest;
);

(lit=r/^["]([^"\\]|([\\].))*["]/).. rest => (
tokens = cons(text[:lit.length], tokens); text = rest;
(id=r/^[a-zA-Z0-9_]+/).. rest => (
tokens = cons(text[:id.length], tokens); text = rest;
);

(comment=r/^#[^\n]*[\n]/).. rest => (
Expand Down
12 changes: 12 additions & 0 deletions tests/c/c-parse.lsts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,18 @@ if true then {
let tokens = std-c-tokenize-string("enum1case", "enum1case");
assert( std-c-parse-constant(tokens).first == Some{CConstantEnumeration{"enum1case"}} );
};
if true then {
let tokens = std-c-tokenize-string("\"abc\"", "\"abc\"");
assert( std-c-parse-string(tokens).first == Some{CConstantString{"\"abc\""}} );
};
if true then {
let tokens = std-c-tokenize-string("u8\"abc\"", "u8\"abc\"");
assert( std-c-parse-string(tokens).first == Some{CConstantString{"u8\"abc\""}} );
};
if true then {
let tokens = std-c-tokenize-string("__func__", "__func__");
assert( std-c-parse-string(tokens).first == Some{CConstantString{"__func__"}} );
};
};

if true then {
Expand Down

0 comments on commit 286346c

Please sign in to comment.