Fox syntax coloring

Joe

Administrator
Staff member
This is what the forum uses to do syntax coloring for many different languages:


And here is the actual script that does the work for VisualBasic:

JavaScript:
Prism.languages['visual-basic'] = {
    'comment': {
        pattern: /(?:['‘’]|REM\b).*/i,
        inside: {
            'keyword': /^REM/i
        }
    },
    'directive': {
        pattern: /#(?:Const|Else|ElseIf|End|ExternalChecksum|ExternalSource|If|Region)(?:[^\S\r\n]_[^\S\r\n]*(?:\r\n?|\n)|.)+/i,
        alias: 'comment',
        greedy: true
    },
    'string': {
        pattern: /["“”](?:["“”]{2}|[^"“”])*["“”]C?/i,
        greedy: true
    },
    'date': {
        pattern: /#[^\S\r\n]*(?:\d+([/-])\d+\1\d+(?:[^\S\r\n]+(?:\d+[^\S\r\n]*(?:AM|PM)|\d+:\d+(?::\d+)?(?:[^\S\r\n]*(?:AM|PM))?))?|(?:\d+[^\S\r\n]*(?:AM|PM)|\d+:\d+(?::\d+)?(?:[^\S\r\n]*(?:AM|PM))?))[^\S\r\n]*#/i,
        alias: 'builtin'
    },
    'number': /(?:(?:\b\d+(?:\.\d+)?|\.\d+)(?:E[+-]?\d+)?|&[HO][\dA-F]+)(?:U?[ILS]|[FRD])?/i,
    'boolean': /\b(?:True|False|Nothing)\b/i,
    'keyword': /\b(?:AddHandler|AddressOf|Alias|And(?:Also)?|As|Boolean|ByRef|Byte|ByVal|Call|Case|Catch|C(?:Bool|Byte|Char|Date|Dbl|Dec|Int|Lng|Obj|SByte|Short|Sng|Str|Type|UInt|ULng|UShort)|Char|Class|Const|Continue|Date|Decimal|Declare|Default|Delegate|Dim|DirectCast|Do|Double|Each|Else(?:If)?|End(?:If)?|Enum|Erase|Error|Event|Exit|Finally|For|Friend|Function|Get(?:Type|XMLNamespace)?|Global|GoSub|GoTo|Handles|If|Implements|Imports|In|Inherits|Integer|Interface|Is|IsNot|Let|Lib|Like|Long|Loop|Me|Mod|Module|Must(?:Inherit|Override)|My(?:Base|Class)|Namespace|Narrowing|New|Next|Not(?:Inheritable|Overridable)?|Object|Of|On|Operator|Option(?:al)?|Or(?:Else)?|Out|Overloads|Overridable|Overrides|ParamArray|Partial|Private|Property|Protected|Public|RaiseEvent|ReadOnly|ReDim|RemoveHandler|Resume|Return|SByte|Select|Set|Shadows|Shared|short|Single|Static|Step|Stop|String|Structure|Sub|SyncLock|Then|Throw|To|Try|TryCast|TypeOf|U(?:Integer|Long|Short)|Using|Variant|Wend|When|While|Widening|With(?:Events)?|WriteOnly|Xor)\b/i,
    'operator': [
        /[+\-*/\\^<=>&#@$%!]/,
        {
            pattern: /([^\S\r\n])_(?=[^\S\r\n]*[\r\n])/,
            lookbehind: true
        }
    ],
    'punctuation': /[{}().,:?]/
};

Prism.languages.vb = Prism.languages['visual-basic'];
 
Last edited:

Joe

Administrator
Staff member
For this forum, I renamed the Visual-Basic.js file to prism-foxpro.js, changed a header and footer, and installed it in the public_html/js/prism_additional directory. Coloring now works. Next I've got to change it from VB to Fox. I wonder if there's a json or xml translator to js. Of course there is...but the results won't work w PrismJS.
 
Last edited:

Joe

Administrator
Staff member
FoxPro:
* There are three types of comments in FoxPro
*        1. A full line comment starting with an asterisk
NOTE     2. A full line comment starting with 'NOTE'
X = 5 && 3. An inline comment starting at '&&' after a command

* All three types of comments can be extended with the FoxPro ;
line extension character, the semicolon.

NOTE example of a 'NOTE' comment extended ;
    with a semicolon.

X = 5 && Example of an inline comment;
         extended to a new line with a semicolon.

* Comments beginning with an asterisk or NOTE cannot be used inline.

     * midline comment
     NOTE another mid-line comment
     && final mid-line comment followed by end of line comments
     *
     NOTE
     &&

NOTE everything should be green except for 'X = 5'

&&& This is a comment
** So is this
* next lines are not:
NOTECOUNT = 5
X = 5 * 5
test = "hello"
lcString = "string within a 'hello' string "
t = "test"
s = &t
IF f = .F.  && had to take out "F|T|N|Y"
    f = .T.
ENDIF
C#:
//* There are three types of comments in FoxPro
//*        1. A full line comment starting with an asterisk
//NOTE     2. A full line comment starting with 'NOTE'
X = 5 //&& 3. An inline comment starting at '&&' after a command
/** All three types of comments can be extended with the FoxPro
line extension character, the semicolon.*/

/*NOTE example of a 'NOTE' comment extended ;
    with a semicolon.*/

X = 5 /*&& Example of an inline comment;
         extended to a new line with a semicolon.*/

//* Comments beginning with an asterisk or NOTE cannot be used inline.
     //* midline comment
     //NOTE another mid-line comment
     //&& final mid-line comment followed by end of line comments
     //*
     //NOTE
     //&&
//NOTE everything should be green except for 'X = 5'

//&&& This is a comment
//** So is this
//* next lines are not:
NOTECOUNT = 5
X = 5 * 5
test = "hello"
t = "test"
s = &t
IF(f == false)
    f = true
 
Last edited:

Joe

Administrator
Staff member
Tools/Options in VFP 8 shows that only comments are green

1584910640381.png

And Keywords are blue:

1584910729247.png

And everything else is 'Automatic' which means no coloring. I checked all the items in the Area dropdown box. This includes: Comments, Keywords, Literals, Normal, Operators, Strings and Variables. Normal?

FoxPro:
* Lucy.prg
USE fred TAG ethel
m.nAmount = 123.45 * 10
m.cName = 'Ricky'
 
Last edited:

Joe

Administrator
Staff member
First pass for code examples in this forum:
FoxPro:
IF|ENDIF|DO|CASE|ENDCASE|UPPER|OTHERWISE|ENDCASE|DEFINE|ENDDEFINE|CLASS|FUNCTION|RETURN|ENDFUNC|AS|DIMENSION|WITH|THIS|TRY|CREATEOBJECT|CATCH|ENDTRY|ENDWITH|PROCEDURE|LPARAMETERS|SQLEXEC|ERROR|LINENO|PROGRAM|TRANSFORM|MESSAGE|ENDPROC|VAL|ALLTRIM|IN|STR
 
Last edited:

Joe

Administrator
Staff member
Foc code below gets all the tokens from Fox 8. (notice the string in line 1 is colorized and should not be, but not after I added a string object to my coloring js)
FoxPro:
USE "C:\Program Files (x86)\Microsoft Visual FoxPro 8\Wizards\fdkeywrd.dbf"
SELECT fdkeywrd
cList = ""
SELECT Code, count(*) FROM fdkeywrd GROUP BY Code INTO CURSOR csrCodes
SELECT csrCodes
SCAN
    cList = cList + "Code: " + csrCodes.Code + CHR(13) + CHR(10)
    SELECT fdkeywrd
    SCAN FOR code = csrCodes.Code
        cList = cList + RTRIM(Token) + " "
    ENDSCAN
    cList = cList + CHR(13) + CHR(10)
ENDSCAN
STRTOFILE(cList, "tokens.prg", 1)
Yield is attached.
 

Attachments

Last edited:

Joe

Administrator
Staff member
String Test with Fox keywords embedded inside of strings:
FoxPro:
USE "C:\Program Files (x86)\Microsoft Visual FoxPro 8\Wizards\fdkeywrd.dbf"
TestString = "C:\Program Files (x86)\Microsoft Visual FoxPro 8\Wizards\fdkeywrd.dbf"
A = B + C
TestString = 'C:\Program Files (x86)\Microsoft Visual FoxPro 8\Wizards\fdkeywrd.dbf'
Problem with single quotes fixed with /g (Global - don't return after first match) Regex option.
 
Last edited:

Joe

Administrator
Staff member
Comments, Keywords, Literals, Operators, Strings and Variables are the possible types.

Test cases of mixes are below:
FoxPro:
*1. Keywords in comments
* DO
*2. Literals in comments
* 345.6
*3. Operators in comments
* 6 * 5
*4. Strings in comments
* "hello world"
*5. Variables in comments
* lcVariable
*6. Operators in strings
lcString = " 6 ***************++++++++++++++ 5 "
*7. Operators in variables (not allowed)
lcVariable = 6
Just lines 12 & 14 should not be comments.
 
Last edited:

Joe

Administrator
Staff member
To do:
FoxPro:
lcString = "string within a ' hello ' string"
* Multiline remarks and non-remarks  ;
that go to the next line
Strings can be delimited by single quotes or double quotes. You can't mix them. If you start w one type of quote char, you must end the string w the same type.

Curly double quote characters are not allowed to be used as string delimiters in Fox. They cause a syntax errer. Straight double quotes are allowed.
 
Last edited:

Joe

Administrator
Staff member
Main tests:
FoxPro:
* There are three types of comments in FoxPro
*        1. A full line comment starting with an asterisk
NOTE     2. A full line comment starting with 'NOTE'
X = 5 && 3. An inline comment starting at '&&' after a command

* All three types of comments can be extended with the FoxPro ;
line extension character, the semicolon.

NOTE example of a 'NOTE' comment extended ; ;
    with a semicolon.

X = 5 && Example of an inline comment;
         extended to a new line with a semicolon.

* Comments beginning with an asterisk or NOTE cannot be used inline.

     * midline comment
     NOTE another mid-line comment
     && final mid-line comment followed by end of line comments
     *
     NOTE
     &&

NOTE everything should be green except for 'X = 5'

&&& This is a comment
** So is this
* next lines are not:
NOTECOUNT = 5
X = 5 * 5
test = "hello"
lcString = "string within a 'hello' string "
t = "test"
s = &t
IF f = .F.  && had to take out "F|T|N|Y" of the keyword object.
    f = .T.
ENDIF

*1. Keywords in comments
* DO
*2. Literals in comments
* 345.6
*3. Operators in comments
* 6 * 5
*4. Strings in comments
* "hello world"
*5. Variables in comments
* lcVariable
*6. Operators in strings
lcString = " 6 ***************++++++++++++++ 5 "
*7. Operators in variables (not allowed)
*lcVari6able = 6

*lcString = “hello” no, these types of quotes are not allowed as delimiters in Fox.
lcString = "hello"
lcString = "string within a ' hello ' string "
lcString = 'string within a " hello " string '
*lcString = "string within a "hello" string" not a valid string
*lcString = 'string within a ' if ' string' not a valid string
*lcString = "string' not a valid string
*lcString = 'string" not a valid string

* some regular Fox code:
USE "C:\Program Files (x86)\Microsoft Visual FoxPro 8\Wizards\fdkeywrd.dbf"
SELECT fdkeywrd
cList = ""
SELECT Code, count(*) FROM fdkeywrd GROUP BY Code INTO CURSOR csrCodes
SELECT csrCodes
SCAN
    cList = cList + "Code: " + csrCodes.Code + CHR(13) + CHR(10)
    SELECT fdkeywrd
    SCAN FOR code = csrCodes.Code
        cList = cList + RTRIM(Token) + " "
    ENDSCAN
    cList = cList + CHR(13) + CHR(10)
ENDSCAN
STRTOFILE(cList, "tokens.prg", 1)

* date test:
ldToday = {^2020/01/01}
llReturn = .F.

* New test - string delimited with square brackets:
lcString = [hello world]
lcString = "hello world"
MESSAGEBOX(lcString)
 
Last edited:
Top