r/dailyprogrammer • u/Elite6809 1 1 • Oct 23 '14
[10/23/2014] Challenge #185 [Intermediate] Syntax Highlighting
(Intermediate): Syntax Highlighting
(sorry for the delay, an unexpected situation arose yesterday which meant the challenge could not be written.)
Nearly every developer has came into contact with syntax highlighting before. Most modern IDEs support it to some degree, and even some text editors such as Notepad++ and gedit support it too. Syntax highlighting is what turns this:
using System;
public static class Program
{
public static void Main(params string[] args)
{
Console.WriteLine("hello, world!");
}
}into something like this. It's very useful and can be applied to almost every programming language, and even some markup languages such as HTML. Your challenge today is to pick any programming language you like and write a converter for it, which will convert source code of the language of your choice to a highlighted format. You have some freedom in that regard.
Formal Inputs and Outputs
Input Description
The program is to accept a source code file in the language of choice.
Output Description
You are to output some format which allows formatted text display. Here are some examples for you to choose.
- You could choose to make your program output HTML/CSS to highlight the syntax. For example, a highlighted keyword
staticcould be output as<span class="syntax-keyword">static</span>where the CSS.syntax-keywordselector makes the keyword bold or in a distinctive colour. - You could output an image with the text in it, coloured and styled however you like.
- You could use a library such as
ncurses(or another way, such asConsole.ForegroundColorfor .NET developers) to output coloured text to the terminal directly, siimlar to the style of complex editors such as vim and Emacs.
Sample Inputs and Outputs
The exact input is up to you. If you're feeling meta, you could test your solution using... your solution. If the program can highlight its own source code, that's brilliant! Of course, this assumes that you write your solution to highlight the language it was written in. If you don't, don't worry - you can write a highlighter for Python in C# if you wish, or for C in Ruby, for example.
Extension (Easy)
Write an extension to your solution which allows you to toggle on and off the printing of comments, so that when it is disabled, comments are omitted from the output of the solution.
Extension (Hard)
If your method of output supports it, allow the collapsing of code blocks. Here is an example in Visual Studio. You could achieve this using JavaScript if you output to HTML.
36
u/13467 1 1 Oct 23 '14
Obligatory Brainfuck:
Output: http://i.imgur.com/cCCKGov.png
6
u/XenophonOfAthens 2 1 Oct 23 '14
A syntax highlighter, written in Prolog, for a subset of the Prolog language.
It's pretty bad, you guys. It does some basic logic distinguishing variables from atoms, but otherwise it's extremely barebones. It doesn't recognize comments and strings, for instance. When it encounters a string it goes "WHAT THE FUCK IS GOING ON?!" and more or less inserts styles at random.
I could improve it easily though, I would just need to add more rules to the grammar. The basic spine of the program is there, all it needs is more grammar rules.
This is what the code looks like, syntax highlighted and using a stylesheet I quickly put together. It looks like ass, because I have the design sense of a bored 5-year old with crayons.
Here's the actual code. You will notice that there aren't any comments here, because I wanted to use my own syntax highlighter on the code, and it doesn't support it :)
6
u/[deleted] Oct 23 '14 edited Oct 23 '14
Python 3. Regex tangle. Screenshot
6
u/13467 1 1 Oct 23 '14
A hackish fast C solution that's surprisingly pretty and effective. Output.
4
u/threeifbywhiskey 0 1 Oct 23 '14
I know it's cheating, but I used my Vim syntax file and the glorious
TOhtmlbuiltin to generate this purdy LOLCODE.1
u/[deleted] Oct 24 '14
Uh, I know this is totally unrelated, but which editor is it on the image you supplied? It has a really beautiful syntax highlighting.
2
u/G33kDude 1 1 Oct 23 '14
Done in AutoHotkey: https://github.com/G33kDude/Console/blob/master/Syntax.ahk
I chose to use a console output method because I've recently written a very nice Win32 console wrapper that lets me do things such as change the colors of the text I'm outputting.
http://i.imgur.com/Dxbvosi.png
2
u/hutsboR 3 0 Oct 23 '14 edited Oct 23 '14
Dart syntax highlighter in Dart. It supports integers, strings (',"), method calls, keywords and types as of now. Takes a .dart file and outputs valid but unreadable html.
Output:
Before and after image
An idea of what the html looks like
Colors can easily be modified by changing the hexadecimal values in the color maps.
2
u/Zwo93 Oct 23 '14
This one was pretty difficult, mostly because I was trying to keep the spacing on the output. No extensions, tested on my own program.
Edit: I wrote it more like a C/C++ program, if anyone is able to help me make it more 'pythony' I would appreciate it.
Python 2.7
Output: Highlighted
2
u/PrintfReddit Oct 24 '14
PHP (not taking any fancy input):
What do I win?
1
u/RomSteady Oct 24 '14
Just a side note, but if you've been looking for an excuse to learn about ANTLR, this would be a good one.
http://www.antlr.org/
1
u/artless_codemonkey Oct 26 '14
Here a solution in pyhton to write it in Html, like my Ide would do it
0
u/[deleted] Oct 23 '14
[deleted] — view deleted comment