Skip to content

Commit

Permalink
Update from master (#8)
Browse files Browse the repository at this point in the history
* Added select and end select.

Still need to add case

* Whitespace

* Update VBATest.php

* Update README.md

* Update README.md

* Remove Case

* Remove case

* Update VBA.php

* Strict is true

* Test to file (#5)

Moving the test case from a string to the cls file.

* Case logic (#6)

* End token (#7)

Added end tokens to case and default

* Update .travis.yml

* Update .travis.yml

* T_SWITCH should open scope

* Update VBA.php

* Update VBATest.php

* Update VBATest.php
  • Loading branch information
Beakerboy committed Mar 9, 2019
1 parent e67b102 commit 14c4dec
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 164 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
A VBA Tokenizer and Coding Standard for PHP_CodeSniffer version 3.x

[![Build Status](https://travis-ci.org/Beakerboy/VBA_Tokenizer.svg?branch=master)](https://travis-ci.org/Beakerboy/VBA_Tokenizer)
[![Coverage Status](https://coveralls.io/repos/github/Beakerboy/VBA_Tokenizer/badge.svg?branch=master)](https://coveralls.io/github/Beakerboy/VBA_Tokenizer?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/Beakerboy/VBA_Tokenizer/badge.png?branch=master)](https://coveralls.io/github/Beakerboy/VBA_Tokenizer?branch=master)

## Manual Installation
Copy the contents of src/Tokenizers and src/Standards to the corresponding PHP-CodeSniffer directories
Expand Down
7 changes: 0 additions & 7 deletions src/Standards/VBA/Sniffs/Whitespace/ScopeIndentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,4 @@ class ScopeIndentSniff extends GenericScopeIndentSniff
public $supportedTokenizers = [
'VBA',
];

/**
* Any scope openers that should not cause an indent.
*
* @var int[]
*/
protected $nonIndentingScopes = [T_SWITCH];
}//end class
259 changes: 158 additions & 101 deletions src/Tokenizers/VBA.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,148 @@

class VBA extends PHP
{
public $scopeOpeners = [
T_IF => [
'start' => [T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET],
'end' => [
T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
],
'strict' => true,
'shared' => false,
'with' => [
T_ELSE => T_ELSE,
T_ELSEIF => T_ELSEIF,
],
],
T_ELSE => [
'start' => [T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET],
'end' => [
T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
],
'strict' => true,
'shared' => false,
'with' => [
],
],
T_ELSEIF => [
'start' => [T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET],
'end' => [
T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
],
'strict' => true,
'shared' => false,
'with' => [
],
],
T_FUNCTION => [
'start' => [T_CLOSE_PARENTHESIS => T_CLOSE_PARENTHESIS], //Should be newline
'end' => [T_ENDDECLARE => T_ENDDECLARE],
'strict' => true,
'shared' => false,
'with' => [],
],
T_WHILE => [
'start' => [T_WHITESPACE => T_WHITESPACE], //Should be newline
'end' => [
T_ENDWHILE => T_ENDWHILE,
T_TRAIT => T_TRAIT,
],
'strict' => true,
'shared' => false,
'with' => [],
],
T_FOREACH => [
'start' => [
T_WHITESPACE=> T_WHITESPACE, //Should be line ending
],
'end' => [
T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
],
'strict' => false,
'shared' => false,
'with' => [],
],
T_FOR => [
'start' => [
T_WHITESPACE=> T_WHITESPACE,
],
'end' => [
T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
],
'strict' => false,
'shared' => false,
'with' => [],
],
T_ABSTRACT => [
'start' => [
T_WHITESPACE=> T_WHITESPACE, //Should be line ending
],
'end' => [
T_CLONE => T_CLONE,
],
'strict' => false,
'shared' => false,
'with' => [],
],
T_SWITCH => [
'start' => [
T_WHITESPACE=> T_WHITESPACE, //Should be line ending
],
'end' => [
T_ENDSWITCH => T_ENDSWITCH,
],
'strict' => true,
'shared' => false,
'with' => [],
],
T_CASE => [
'start' => [
T_WHITESPACE=> T_WHITESPACE, //Should be line ending
],
'end' => [
T_BREAK => T_BREAK,
],
'strict' => true,
'shared' => true,
'with' => [
T_CASE => T_CASE,
T_SWITCH => T_SWITCH,
T_DEFAULT => T_DEFAULT,
],
],
T_DEFAULT => [
'start' => [
T_WHITESPACE=> T_WHITESPACE, //Should be line ending
],
'end' => [
T_BREAK => T_BREAK,
],
'strict' => true,
'shared' => true,
'with' => [
T_CASE => T_CASE,
T_SWITCH => T_SWITCH,
],
],
];

/**
* A list of tokens that end the scope.
*
* This array is just a unique collection of the end tokens
* from the scopeOpeners array. The data is duplicated here to
* save time during parsing of the file.
*
* @var array
*/
public $endScopeTokens = [
T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
T_ENDIF => T_ENDIF,
T_ENDFOR => T_ENDFOR,
T_ENDFOREACH => T_ENDFOREACH,
T_ENDWHILE => T_ENDWHILE,
T_ENDSWITCH => T_ENDSWITCH,
];

protected function convertFile($string)
{
Expand All @@ -24,12 +166,16 @@ protected function convertFile($string)
$token[1] = 'function';
} elseif ($token[1] == 'BEGIN') {
$token[1] = 'abstract';
} elseif ($token[1] == 'Select') {
$token[1] = 'switch';
unset($line_tokens[$key + 1]);
unset($line_tokens[$key + 2]);
} elseif ($token[1] == 'Not') {
$token[1] = '!';
} elseif ($token[1] == 'Then') {
$token = [T_STRING, ') {'];
} elseif ($token[1] == 'Wend') {
$token[1] = 'static';
$token[1] = 'endwhile';
} elseif ($token[1] == 'Loop') {
$token[1] = 'trait';
} elseif ($token[1] == 'Is') {
Expand All @@ -52,6 +198,10 @@ protected function convertFile($string)
$token[1] = '}';
unset($line_tokens[$key + 1]);
unset($line_tokens[$key + 2]);
} elseif ($next_tag == 'Select') {
$token[1] = 'endswitch';
unset($line_tokens[$key + 1]);
unset($line_tokens[$key + 2]);
}
}
} elseif ($token[0] === T_IF) {
Expand All @@ -60,8 +210,12 @@ protected function convertFile($string)
$token = [T_STRING, '} else {'];
} elseif ($token[0] === T_ELSEIF) {
$token = [T_STRING, '} elseif ('];
// } elseif ($token[0] == T_BITWISE_AND) {
// $token[1] = '.';
} elseif ($token[0] === T_CASE) {
if ($line_tokens[$key + 2][0] === T_ELSE) {
$token[1] = 'default';
unset($line_tokens[$key + 1]);
unset($line_tokens[$key + 2]);
}
} elseif ($token == '.') {
$token = [T_STRING, '->'];
} elseif ($token[0] == T_FOR) {
Expand Down Expand Up @@ -97,105 +251,8 @@ protected function convertFile($string)
*/
protected function tokenize($string)
{
$this->scopeOpeners[T_CLASS] =
[
'start' => [T_STRING => T_STRING],
'end' => [T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET],
'strict' => true,
'shared' => false,
'with' => [],
];
$this->scopeOpeners[T_IF] =
[
'start' => [T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET],
'end' => [
T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
],
'strict' => true,
'shared' => false,
'with' => [
T_ELSE => T_ELSE,
T_ELSEIF => T_ELSEIF,
],
];
$this->scopeOpeners[T_ELSE] =
[
'start' => [T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET],
'end' => [
T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
],
'strict' => true,
'shared' => false,
'with' => [
],
];
$this->scopeOpeners[T_ELSEIF] =
[
'start' => [T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET],
'end' => [
T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
],
'strict' => true,
'shared' => false,
'with' => [
],
];
$this->scopeOpeners[T_FUNCTION] =
[
'start' => [T_CLOSE_PARENTHESIS => T_CLOSE_PARENTHESIS], //Should be newline
'end' => [T_ENDDECLARE => T_ENDDECLARE],
'strict' => true,
'shared' => false,
'with' => [],
];
$this->scopeOpeners[T_WHILE] =
[
'start' => [T_WHITESPACE => T_WHITESPACE], //Should be newline
'end' => [
T_STATIC => T_STATIC,
T_TRAIT => T_TRAIT,
],
'strict' => true,
'shared' => false,
'with' => [],
];
$this->scopeOpeners[T_FOREACH] =
[
'start' => [
T_WHITESPACE=> T_WHITESPACE, //Should be line ending
],
'end' => [
T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
],
'strict' => false,
'shared' => false,
'with' => [],
];
$this->scopeOpeners[T_FOR] =
[
'start' => [
T_WHITESPACE=> T_WHITESPACE,
],
'end' => [
T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
],
'strict' => false,
'shared' => false,
'with' => [],
];
$this->scopeOpeners[T_ABSTRACT] =
[
'start' => [
T_WHITESPACE=> T_WHITESPACE, //Should be line ending
],
'end' => [
T_CLONE => T_CLONE,
],
'strict' => false,
'shared' => false,
'with' => [],
];
$new_string = $this->convertFile($string);

return parent::tokenize($new_string);
}
}
11 changes: 7 additions & 4 deletions tests/Test.cls
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Test"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True

Option Explicit

' Class: Test
Expand All @@ -21,6 +18,12 @@ Public Function Foo(iVariable As Double) As Boolean
While iVariable Is 2
iVariable = iVariable + 1
Wend
Select Case iVariable
Case iVariable Is 3
Foo = 3
Case Else
Foo = 4
End Select
End Function

' Function: Bar
Expand Down

0 comments on commit 14c4dec

Please sign in to comment.