Skip to content

Commit

Permalink
lexer,example,misc: add keywords up to solc v0.6.0.
Browse files Browse the repository at this point in the history
Everything in one lazy blob. Sorry.
  • Loading branch information
veox committed Dec 23, 2019
1 parent 62f6dda commit bb2ea61
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 22 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
python-lexer-solidity change log
================================

[v0.4.0] - 2019-12-23
---------------------
Changed
^^^^^^^
* Updated keywords to those of ``solc`` v0.6.0.


[v0.3.1] - 2018-05-24
---------------------
Changed
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2016-2018 Noel Maersk
Copyright (c) 2016-2019 Noel Maersk
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
11 changes: 10 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Solidity lexer for Pygments, distributed as a PyPI package.
If you want pretty syntax highlighting in documentation for Solidity
files, and you're using Pygments, this might just be the thing for you.

Currently, Solidity keywords up to version 0.4.22 are included, to the
Currently, Solidity keywords up to version 0.6.0 are included, to the
best of my ability. MRs are welcome!


Expand Down Expand Up @@ -34,6 +34,15 @@ Have this in Sphinx's ``conf.py``:
Then use ``.. code-block:: solidity`` for Solidity code blocks.

Command-line
^^^^^^^^^^^^

If you just want to test a local copy of the lexer on the CLI:

.. code-block:: shell
% pygmentize -x -l pygments_lexer_solidity/lexer.py:SolidityLexer example.sol
License
-------
Expand Down
33 changes: 18 additions & 15 deletions example.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pragma solidity ^0.4.18;
pragma experimental "v0.5.0";
pragma experimental ABIEncoderV2;
pragma solidity ^0.6.0;
pragma ABIEncoderV2;
pragma experimental SMTChecker;

/**********************************************************************
Expand All @@ -15,7 +14,7 @@ pragma experimental SMTChecker;
/* Comments relevant to the code are multi-line. */

library Assembly {
function junk(address _addr) private returns (address _ret) {
public function junk(address _addr) private returns (address _ret) {
assembly {
let tmp := 0

Expand Down Expand Up @@ -52,11 +51,17 @@ and it's multi-line. // no comment"; // comment ok // even nested :)
and it\'s multi-line. // no comment'; // same thing, single-quote
string hexstr = hex'537472696e67732e73656e6428746869732e62616c616e6365293b';

function(){}
fallback() external {}

receive() external payable {
revert();
}
}

contract Types is Strings {
using Assembly for Assembly;

bytes stringsruntime = type(Strings).runtimeCode;

// typesM (compiler chokes on invalid)
int8 i8; // valid
Expand Down Expand Up @@ -88,7 +93,7 @@ contract Types is Strings {
}
mapping (address => AddressMap) touchedMe;

function failOnNegative(int8 _arg)
public function failOnNegative(int8 _arg)
private
constant
returns (uint256)
Expand All @@ -98,15 +103,15 @@ contract Types is Strings {
}

// some arithmetic operators + built-in names
function opportunisticSend(address k) private {
public function opportunisticSend(address k) private {
/* `touchedMe[k].result` et al are addresses, so
`send()` available */
touchedMe[k].origin.send(k**2 % 100 finney);
touchedMe[k].result.send(1 wei);
touchedMe[k].sender.send(mulmod(1 szabo, k, 42));
}

function() payable {
fallback() external payable {
/* inferred type: address */
var k = msg.sender;
/* inferred type: `ufixed0x256` */
Expand Down Expand Up @@ -172,15 +177,13 @@ contract BadPractices {
mutex = false;
}

/* constructor */
function BadPractices() {
constructor {
creator = tx.origin;
owner = msg.sender;
}

/* Dangerous - function public (by default), and doesn't check
who's calling. */
function withdraw(uint _amount)
/* Dangerous - function public, and doesn't check who's calling. */
public function withdraw(uint _amount)
critical
returns (bool)
{ /* `mutex` set via modifier */
Expand All @@ -191,7 +194,7 @@ contract BadPractices {
} /* `mutex` reset via modifier */

/* fallback */
function () payable {
fallback() external payable {
/* `i` will be `uint8`, so this is an endless loop
that will consume all gas and eventually throw.
*/
Expand All @@ -214,7 +217,7 @@ contract MoreBadPractices is BadPractices {
\* /
/ * no modifiers to check ownership * /
function () payable {
fallback() external payable {
balance += msg.value;
/ * vulnerable to re-entry * /
Expand Down
24 changes: 20 additions & 4 deletions pygments_lexer_solidity/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ def type_names_mn(prefix, sizerangem, sizerangen):
# everything else is either a local/external var, or label
('[a-zA-Z_]\w*', Name)
],
# FIXME: not used!
# TODO: Yul parsing (not implemented ATM)
#'yul': [],
'natspec': [
(r'@(author|dev|notice|param|return|title)\b', Comment.Special),
],
Expand All @@ -115,7 +116,7 @@ def type_names_mn(prefix, sizerangem, sizerangen):
'keywords-other': [
(words(('for', 'in', 'while', 'do', 'break', 'return',
'returns', 'continue', 'if', 'else', 'throw',
'new', 'delete'),
'new', 'delete', 'try', 'catch'),
suffix=r'\b'), Keyword),

(r'assembly\b', Keyword, 'assembly'),
Expand All @@ -127,8 +128,9 @@ def type_names_mn(prefix, sizerangem, sizerangen):

(r'(import|using)\b', Keyword.Namespace),

# pragmas are not pragmatic in their formatting :/
(r'pragma( experimental| solidity|)\b', Keyword.Reserved),
# misc keywords
(r'pragma (solidity|experimental)\b', Keyword.Reserved),
(r'(_|as|constant|default|from|is)\b', Keyword.Reserved),
(r'emit\b', Keyword.Reserved),
# built-in modifier
Expand All @@ -139,8 +141,19 @@ def type_names_mn(prefix, sizerangem, sizerangen):
(r'(external|internal|private|public)\b', Keyword.Reserved),
# event parameter specifiers
(r'(anonymous|indexed)\b', Keyword.Reserved),
# added in `solc v0.4.0`, not covered elsewhere
# added in solc v0.4.0, not covered elsewhere
(r'(abstract|pure|static|view)\b', Keyword.Reserved),
# access to contracts' codes and name
(r'type\(.*\)\.(creationCode|runtimeCode|name)\b', Keyword.Reserved),

# reserved for future use since solc v0.5.0
(words(('alias', 'apply', 'auto', 'copyof', 'define', 'immutable',
'implements', 'macro', 'mutable', 'override', 'partial',
'promise', 'reference', 'sealed', 'sizeof', 'supports',
'typedef', 'unchecked'),
suffix=r'\b'), Keyword.Reserved),
# reserved for future use since solc v0.6.0
(r'virtual\b', Keyword.Reserved),

# built-in constants
(r'(true|false)\b', Keyword.Constant),
Expand Down Expand Up @@ -224,6 +237,9 @@ def type_names_mn(prefix, sizerangem, sizerangen):
(r'(this|super)\b', Name.Builtin),
(r'selector\b', Name.Builtin),

# receive/fallback functions
(r'(receive|fallback)\b', Keyword.Function),

# like block.hash and msg.gas in `keywords-nested`
(r'(blockhash|gasleft)\b', Name.Function),

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def read(fname):

setup(
name='pygments-lexer-solidity',
version='0.3.1',
version='0.4.0',
description='Solidity lexer for Pygments',
long_description=read('README.rst'),
license="BSD",
Expand Down

0 comments on commit bb2ea61

Please sign in to comment.