Skip to content

Commit

Permalink
Allow char literal convertion into unsigned (#82)
Browse files Browse the repository at this point in the history
required to compile stm32mp1xxx dtb

Also includes tests.

Fixes #81
  • Loading branch information
ssilnicki-dev committed Nov 1, 2023
1 parent 39a58cf commit 23387dd
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Tests/char_literals.dts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/dts-v1/;

/ {

gpio_port = <('F' - 'A')>;
test_nl = <('\n')>;
test_cr = <('\r')>;
test_tab = <('\t')>;
test_bs = <('\\')>;
test_sq = <('\'')>;
test_zero = <('\0')>;

}
12 changes: 12 additions & 0 deletions Tests/char_literals.dts.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/dts-v1/;

/ {

gpio_port = <0x5>;
test_nl = <0xa>;
test_cr = <0xd>;
test_tab = <0x9>;
test_bs = <0x5c>;
test_sq = <0x27>;
test_zero = <0x0>;
};
53 changes: 53 additions & 0 deletions input_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,47 @@ input_buffer::consume(const char *str)
return false;
}

bool
input_buffer::consume_char_literal(unsigned long long &outInt)
{
outInt = (unsigned char)((*this)[0]);
cursor++;

if(outInt != '\\')
{
return true;
}
else if(cursor >= size)
{
return false;
}

outInt = (unsigned char)((*this)[0]);
cursor++;

switch (outInt) {
default:
return false;
case 'n':
outInt = (unsigned char)'\n';
break;
case 'r':
outInt = (unsigned char)'\r';
break;
case 't':
outInt = (unsigned char)'\t';
break;
case '0':
outInt = 0;
break;
case '\'':
case '\\':
break;
}

return true;
}

bool
input_buffer::consume_integer(unsigned long long &outInt)
{
Expand Down Expand Up @@ -874,6 +915,18 @@ expression_ptr text_input_buffer::parse_expression(bool stopAtParen)
source_location l = location();
switch (*(*this))
{
case '\'':
consume('\'');
if(!consume_char_literal(leftVal))
{
return nullptr;
}
if (!consume('\''))
{
return nullptr;
}
lhs.reset(new terminal_expr(l, leftVal));
break;
case '0'...'9':
if (!consume_integer(leftVal))
{
Expand Down
22 changes: 22 additions & 0 deletions input_buffer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ class input_buffer
* current point in the input.
*/
bool consume(const char *str);
/**
* Reads unsigned from char literal. Returns true and advances
* the cursor to next char.
*
* The parsed value is returned via the argument.
*/
bool consume_char_literal(unsigned long long &outInt);
/**
* Reads an integer in base 8, 10, or 16. Returns true and advances
* the cursor to the end of the integer if the cursor points to an
Expand Down Expand Up @@ -412,6 +419,21 @@ class text_input_buffer
}
return input_stack.top()->consume(str);
}
/**
* Converts next char into unsigned
*
* The parsed value is returned via the argument.
*
* This method does not scan between files.
*/
bool consume_char_literal(unsigned long long &outInt)
{
if (input_stack.empty())
{
return false;
}
return input_stack.top()->consume_char_literal(outInt);
}
/**
* Reads an integer in base 8, 10, or 16. Returns true and advances
* the cursor to the end of the integer if the cursor points to an
Expand Down

0 comments on commit 23387dd

Please sign in to comment.