Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix solidity lexer not picking up many of the operators (e.g. boolean and) #2292

Merged
merged 2 commits into from Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pygments/lexers/solidity.py
Expand Up @@ -59,7 +59,7 @@ class SolidityLexer(RegexLexer):
(datatype, Keyword.Type),
include('constants'),
(r'[a-zA-Z_]\w*', Text),
(r'[!<=>+*/-]', Operator),
(r'[~!%^&*+=|?:<>/-]', Operator),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

: is now recognized as both Operator and Punctuation - which makes more sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch. it is somewhat ambiguous. : can be used in the shorthand if operator a ? b : c or also in structs like {x : y}. I would stick with Operator.

(r'[.;:{}(),\[\]]', Punctuation)
],
'comments': [
Expand Down
92 changes: 69 additions & 23 deletions tests/examplefiles/solidity/test.sol
Expand Up @@ -11,7 +11,6 @@ pragma solidity >=0.4.0 <0.7.0;
*/

contract ContractName {

address public publicaddress;

uint varname1 = 1234;
Expand All @@ -20,16 +19,20 @@ contract ContractName {
string astringsingle = 'test "string" value\' single';
string astringdouble = "test 'string' value\" double";

address public shipper;
address private arbiter;
address astronaut;

enum State {
NotStarted,
WorkInProgress,
Done
NotStarted,
WorkInProgress,
Done
}
State public state;

struct AStruct {
string name;
uint8 type;
uint8 atype;
}

mapping(address => AStruct) registry;
Expand All @@ -39,36 +42,79 @@ contract ContractName {
event Withdraw(uint256 value);

function addRegistry(string _name, uint8 _type) {
AStruct memory newItem = AStruct({
name: _name,
type: _type
});
AStruct memory newItem = AStruct({name: _name, atype: _type});

registry[msg.sender] = newItem;
}

function getHash(AStruct item) returns(uint) {
return uint(keccak256(item.name, item.type));
function getHash(AStruct item) returns (uint) {
return uint(keccak256(item.name, item.atype));
}

function pay() public payable {
require(msg.sender == astronaut);
state = State.Paid;
Paid(msg.value);
require(msg.sender == astronaut);
state = State.Paid;
Paid(msg.value);
}

function receive() public {
require(msg.sender == arbiter);
require(state == State.Paid);
state = State.Received;
Received(now);
require(msg.sender == arbiter);
require(state == State.Paid);
state = State.Received;
Received(now);
}

function withdraw() public {
require(msg.sender == shipper);
require(state == State.Received);
state = State.Withdrawn;
Withdraw(this.balance);
shipper.transfer(this.balance);
require(msg.sender == shipper);
require(state == State.Received);
state = State.Withdrawn;
Withdraw(this.balance);
shipper.transfer(this.balance);
}

function max(uint a, uint b) public returns (uint) {
if (a > b && b < a) {
return a;
} else {
return b;
}
}

function operators() public {
uint a = 20;
uint b = 10;
// arithmetic
uint _sum = a + b;
uint _diff = a - b;
uint _mul = a * b;
uint _div = a / b;
uint _mod = a % b;
uint _dec = --b;
uint _inc = ++a;

// comparison
bool _eq = a == b;
bool _noteq = a != b;
bool _greater = a > b;
bool _less = a < b;
bool _geq = a >= b;
bool _leq = a <= b;

// logical boolean
bool x = true;
bool y = false;
bool _and = x && y;
bool _or = a || b;
bool _not = !a;

// bitwise
uint64 i = 20;
uint64 j = 10;
uint64 _bitand = i & j;
uint64 _bitor = i | j;
uint64 _bitxor = i ^ j;
uint64 _leftshift = i << j;
uint64 _rightshift = i >> j;
uint64 _bitnot = ~i;
}
}