New USHF Grammars

New Universal Syntax Highlighting Grammars.

Posted on: 220307

In this next installment, we're looking at the grammars that come with the Universal Syntax Highlighting Function and the new one's I have just added.

HTML

I mentioned in the previous article that the grammar for HTML is only 7 lines of code. Well, here it is...

HSYN.HTML = [ // HTML syntax rules... /* 0. TOP */{ cx:0, ch:[1,2] }, /* 1. ENTITY */{ cl:'ents', os:'&', nd:';', cx:' \r\n\t' }, /* 2. TAGSPACE */{ cl:'tags', os:'<', nd:'>', ch: [3,4,5,6] }, /* 3. ATTRIBUTES */{ cl:'attrs', oc:' ', cx:'=>/"\'' }, /* 4. S QUOTES */{ cl:'quote', os:"'", nd:"'", ch:[1] }, /* 5. D QUOTES */{ cl:'quote', os:'"', nd:'"', ch:[1] }, /* 6. DTD SPACE */{ cl:'dtd', os:'!', cx:'>', ch:[7] }, /* 7. COMMENT */{ cl:'comm', os:'--', nd:'--' } ];

This is enough to cover the 8 scopes of HTML.

Javascript

The one for Javascript is a bit meatier, though by no means are the dictionaries for different terms complete...

HSYN.JS = [ // JavaScript syntax rules... /* 0. TOP */{ cx:0, ch:[1,2,3,4,5,6,7] }, /* 1. NUMBERS */{ cl:'nums', oc:'0123456789', cp:'0123456789' }, /* 2. S QUOTES */{ cl:'quote', os:"'", nd:"'", ch:[4] }, /* 3. D QUOTES */{ cl:'quote', os:'"', nd:'"', ch:[4] }, /* 4. ESCAPES */{ cl:'escp', os:'\\', lm:2 }, /* 5. L COMMENT */{ cl:'comm', os:'//', cx:'\r\n' }, /* 6. B COMMENT */{ cl:'comm', os:'/*', nd:'*/' }, /* 7. WORDS */{ cl:'word', ch:[4], oc:'_$ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', cx:' \t\r\n.,:;+-*/\\%^<>=#&|!~\'"()[]{}?', id:[ ['.keyw', // language vocabulary: 'if','else','switch','break','case','while','for','continue', 'try','throw','catch','goto','with','in','do','var','new', 'import','finally','function','return','let','export','typeof', 'instanceof','default','throws','window','document','arguments' ], ['.null', // falsey things: 'false','null','undefined','NaN','alert' ], ['.true', // truthy things: 'true','Infinity','this','Object','Array','Math','String','Date', 'Number' ], ['.term', // common terms: 'length','children','innerHTML','textContent','charAt','charCodeAt', 'parentNode','previousSibling','nextSibling','substr','replace', 'getAttribute','setAttribute','getHours','getMinutes','getSeconds', 'getMilliseconds','getDay','getDate','getMonth','getYear', 'innerHeight','innerWidth','outerHeight','outerWidth','indexOf', 'fromCharCode','getElementsByTagName','toUpperCase','toLowerCase', 'style','display','width','height','src','style','endsWith','startsWith', 'valueOf','pageXOffset','pageYOffset','screenX','screenY','scroll', 'setInterval','setTimeout','clearInterval','clearTimeout', 'encodeURI','decodeURI','floor','ceil','random','from', 'delete','parseInt','toString','setInterval','isPrototypeOf', 'encodeURIComponent','parseFloat','createElement','head','body', 'push','pop','shift','unshift','appendChild','insertBefore', 'onmousemove','onmousedown','onmouseup','onscroll','onresize', 'clientX','clientY','getBoundingClientRect','nodeType','which', 'left','top','bottom','right','scrollLeft','scrollTop','detail', 'documentElement','wheelDelta','offsetTop','offsetLeft','scrollWidth', 'offsetWidth','offsetHeight','getElementById','scrollHeight', 'getContext','getImageData','toDataURL','putImageData','drawImage', 'contentWindow' ] ]} ];

There's also CSS and PHP already included, but I won't bother detailing them. PHP is almost the same as Javascript and CSS is mostly just terms.

New Language Arrays

So I have begun to add new language arrays to the Universal Syntax Highlighting Function. As many as I can immediately, and I'll be adding more periodically as I learn new programming languages...

C

/* C language... */ #include <stdio.h> #include <stdlib.h> int main(int argc, char** argv){ int *arr = malloc (sizeof(int) * 100); int i = 0; while(i<100){ arr[i] = i; i++; } return 0; }

Shell Script

if [ "$1" = "log" ]; then shift case "$WPA_ACTION" in "CONNECTED"|"DISCONNECTED") [ -x /usr/bin/logger ] || return if [ "$#" -gt 0 ]; then logger -t "wpa_action" "$@" else logger -t "wpa_action" fi ;; *) [ "$#" -gt 0 ] && echo "wpa_action: $@" ;; esac return fi

SQL

-- SUM of call duration per each employee SELECT employee.id, employee.first_name, employee.last_name, SUM(DATEDIFF("SECOND", call.start_time, call.end_time)) AS call_duration_sum FROM call INNER JOIN employee ON call.employee_id = employee.id GROUP BY employee.id, employee.first_name, employee.last_name ORDER BY employee.id ASC;

Java

package com.domain.package; import android.view.View; public class MyView extends View { public Context ctx; public MyView(Context C){ super(C); ctx = C; } private int[] nums = new int[30]; public int getNumIndex(n){ int i; for(i=0;i<30;i++){ if(nums[i]==n){ return i; } } return -1; } }

XML

I actually just expanded the HTML array to include XML declarations...

<?xml version="1.0"?> <rss version="2.0"> <channel> <title>Diagonal Symmetry</title> <link>https://diagonalsymmetry.com</link> <item> <title>New USHF Grammars</title> <link>https://diagonalsymmetry.com/220307</link> <pubDate>Mon, 07 Mar 2022 11:11:11 MST</pubDate> <description>Introducing new grammars for the Universal Syntax Highlighting Function.</description> </item> </channel> </rss>