From ecad8a63e2e594a3cfacc736accf465b23922efc Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 14 May 2017 16:28:15 +0200 Subject: [PATCH] added AUTOMATE base code --- src/Makefile | 18 +++++++- src/lib/automate/automate.c | 83 +++++++++++++++++++++++++++++++++++++ src/lib/automate/automate.h | 55 ++++++++++++++++++++++++ src/lib/common/common.c | 64 ++++++++++++++++++++++++++++ src/lib/common/common.h | 44 ++++++++++++++++++++ src/lib/php-const.h | 35 +++++++++------- 6 files changed, 281 insertions(+), 18 deletions(-) create mode 100644 src/lib/automate/automate.c create mode 100644 src/lib/automate/automate.h create mode 100644 src/lib/common/common.c create mode 100644 src/lib/common/common.h diff --git a/src/Makefile b/src/Makefile index 204cacd..dbaccf6 100644 --- a/src/Makefile +++ b/src/Makefile @@ -7,11 +7,25 @@ compile: install install: linter +clean: + rm *.o; + rm lib/*.o; + rm linter; + +lib/common.o: lib/common/common.h lib/common/common.c + gcc -c -o lib/common.o lib/common/common.c; + +lib/automate.o: lib/automate/automate.h lib/automate/automate.c + gcc -c -o lib/automate.o lib/automate/automate.c; + +lib/scope.o: lib/scope/scope.h lib/scope/scope.c + gcc -c -o lib/scope.o lib/scope/scope.c; + linter.o: linter.h linter.c gcc -c -o linter.o linter.c; -linter: linter.o - gcc $(GCCARGV) -o linter linter.o; +linter: lib/scope.o lib/automate.o lib/common/common.o linter.o + gcc $(GCCARGV) -o linter lib/automate.o lib/scope.o lib/common/common.o linter.o; diff --git a/src/lib/automate/automate.c b/src/lib/automate/automate.c new file mode 100644 index 0000000..b06aca7 --- /dev/null +++ b/src/lib/automate/automate.c @@ -0,0 +1,83 @@ +/************************** +* Automate * +*************************** +* Designed & Developed by * +* Adrien Marquès * +* * +*************************** +* doowap31@gmail.com * +**************************/ +#include "automate.h" + + + + +int browse(struct Automate pAutomate, const char* pString){ + + /* [0] Initialize variables + =========================================================*/ + unsigned int strIndex = 0; // String current char index + int c, len, i, l; // counters + + + /* [1] For each char + =========================================================*/ + c = 0; len = strlen(pString); + + for( ; c < len ; c++ ){ + + /* (1) Check if the current char can lead to a state + ---------------------------------------------------------*/ + /* (1) Check current state choices */ + i = 0; l = pAutomate.states[pAutomate.state].n; + + for( ; i < l ; i++ ){ + + /* (2) If part of set */ + if( is_part(pString[strIndex], pAutomate.set[pAutomate.states[pAutomate.state].set[i]]) == 1 ){ + pAutomate.state = pAutomate.states[pAutomate.state].state[i]; + strIndex++; + } + + } + + } + + return pAutomate.state; + + +} + + + + + + + + + + + + + + + + + + + + + +char is_part(const char pChar, const char* pSet){ + + unsigned int a; + unsigned int A = strlen(pSet); + + /* [1] For each char of @pSet + =========================================================*/ + for( a = 0 ; a < A ; a++ ) + if( pChar == pSet[a] ) + return 1; + + return 0; +} \ No newline at end of file diff --git a/src/lib/automate/automate.h b/src/lib/automate/automate.h new file mode 100644 index 0000000..006740a --- /dev/null +++ b/src/lib/automate/automate.h @@ -0,0 +1,55 @@ +/************************** +* Automate * +*************************** +* Designed & Developed by * +* Adrien Marquès * +* * +*************************** +* doowap31@gmail.com * +**************************/ +#ifndef _LIB_AUTOMATE_AUTOMATE_H_ + #define _LIB_AUTOMATE_AUTOMATE_H_ + + #include + + + struct AutomateState{ + unsigned int n; // number of associations + char* set; // list of set_id + char* state; // list of state_id + }; + + struct Automate{ + struct AutomateState* states; // states + unsigned int n_state; // Max state index + unsigned int state; // current state index + unsigned int n_set; // Max set index + char** set; // list of sets + }; + + + /* Try to browse an automate with a string + * + * @pAutomate The current automate + * @pString The string to test + * + * + * @return state The final state we can browse to + * + */ + int browse(struct Automate pAutomate, const char* pString); + + + + /* Checks if a char is part of a set + * + * @pChar The char to test + * @pSet The set to test in + * + * @return is_part 1 if is part, 0 if not + * + */ + char is_part(const char pChar, const char* pSet); + + +#endif \ No newline at end of file diff --git a/src/lib/common/common.c b/src/lib/common/common.c new file mode 100644 index 0000000..82aa49d --- /dev/null +++ b/src/lib/common/common.c @@ -0,0 +1,64 @@ +/************************** +* Common * +*************************** +* Designed & Developed by * +* Adrien Marquès * +* * +*************************** +* doowap31@gmail.com * +**************************/ +#include "common.h" + +int isolate_line(char* pLine, const size_t pCount){ + + unsigned int start = 0; + unsigned int end = 0; + + /* [1] Copy input line + =========================================================*/ + char input[pCount+1]; + strcpy(input, pLine); + + /* [2] Remove surrounding whitespace + =========================================================*/ + /* (1) Starting whitespace */ + while( input[start] == ' ' || input[start] == '\t' ) start++; + + /* (2) Trailing whitespace */ + while( input[pCount-end] == ' ' || input[pCount-end] == '\t' ) end++; + + + /* [3] Copy isolated in buffer + =========================================================*/ + bzero(pLine, pCount); + sprintf(pLine, "%.*s", (int)pCount-end-start, input+start); + return 0; + +} + + + +int getIndentation(char* pLine, const size_t pCount){ + + unsigned int c; + unsigned int depth = 0; + + + /* [1] Count number of tabs + =========================================================*/ + for( c = 0 ; c < pCount ; c++ ) + if( pLine[c] == '\t' ) depth++; + else break; + + /* [2] Count number of 4x spaces + =========================================================*/ + if( depth == 0 ){ + for( c = 0 ; c < pCount ; c++ ) + if( pLine[c] == ' ' ) depth++; + else break; + + depth /= 4; + } + + return depth; +} \ No newline at end of file diff --git a/src/lib/common/common.h b/src/lib/common/common.h new file mode 100644 index 0000000..0260ef6 --- /dev/null +++ b/src/lib/common/common.h @@ -0,0 +1,44 @@ +/************************** +* Common * +*************************** +* Designed & Developed by * +* Adrien Marquès * +* * +*************************** +* doowap31@gmail.com * +**************************/ +#ifndef _LIB_COMMON_COMMON_H_ + #define _LIB_COMMON_COMMON_H_ + + /* GLOBAL */ + #include + #include + #include + + /* LOCAL */ + + /* SIGNATURES */ + + /* [1] Isolates the useful content of a line + * + * @pLine Input line char array + * @pCount Line length (strlen) + * + * @return status 0 if Success, -1 on failure + * + */ + int isolate_line(char* pLine, const size_t pCount); + + + /* [2] Get indentation depth + * + * @pLine Input line char array + * @pCount Line length (strlen) + * + * @return depth Depth or -1 on failure + * + */ + int getIndentation(char* pLine, const size_t pCount); + + +#endif \ No newline at end of file diff --git a/src/lib/php-const.h b/src/lib/php-const.h index 27ef374..f3de019 100644 --- a/src/lib/php-const.h +++ b/src/lib/php-const.h @@ -10,19 +10,21 @@ #ifndef _LIB_PHP_CONST_H_ #define _LIB_PHP_CONST_H_ - typedef enum PHP_SCOPE{ + #define MAX_LINE 512 + + typedef enum{ PHP = 0x00, // - FUNCTION = 0x01, - CLASS = 0x02, + FUNCTION = 0x01, // function name(){ ... } + CLASS = 0x02, // class name{ ... } WHILE = 0x04, FOR = 0x08, FOREACH = 0x10, IF = 0x20, ELSE = 0x40, ELSIF = 0x80 - }; + } PHP_SCOPE; - typedef enum PHP_STATEMENT{ + typedef enum{ VOID = 0x0000, // \r?\n DECLARE_VAR = 0x0001, // \s?\$(\w+)(\[(?:["']\w+["']|\$\w+)\])*; ASSIGN_VAR = 0x0002, // \s?\$(\w+)\s*([\.\+\/\*|&-]?=)\s*(.+); @@ -35,31 +37,32 @@ COMMENT = 0x0100, // //, /**/, ... RETURN = 0x0200, // return ... ECHO = 0x0400, // echo "", echo $var, ... - DEPENDENCY = 0x0800 // require_once, include, require, ... - PROPERTY = 0x1000 // require_once, include, require, ... + DEPENDENCY = 0x0800, // require_once, include, require, ... + PROPERTY = 0x1000, // require_once, include, require, ... METHOD = 0x2000 // require_once, include, require, ... - }; + } PHP_STATEMENT; - typedef enum PHP_COMMENT{ + typedef enum{ INLINE = 0x001, MULTILINE = 0x002, CHAPTER = 0x004, SUBCHAPTER = 0x008, SECTION = 0x010, SUBSECTION = 0x020, - FUNCTION_HEAD = 0x040, - FUNCTION_CHAPTER_HEAD = 0x080, - FUNCTION_SUBCHAPTER_HEAD = 0x100, + FUNC_HEAD = 0x040, + FUNC_CHAPTER_HEAD = 0x080, + FUNC_SUBCHAPTER_HEAD = 0x100, TITLE = 0x200, HEADER = 0x400 - }; + } PHP_COMMENT; - typedef enum PHP_TYPE{ - NULL = 0x00, // null + typedef enum{ + NUL = 0x00, // null STRING = 0x01, // ".+" | '.+' | STRING.STRING INT = 0x02, // -?\d+ FLOAT = 0x04, // -?\d*.\d+ ARRAY = 0x08 // \[((, )?(PHP_TYPE|CLASS|FUNC|VAR))\] - }; + } PHP_TYPE; + #endif \ No newline at end of file