Skip to content
This repository was archived by the owner on Apr 22, 2020. It is now read-only.

Add Ada support #601

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ etc. without a language extension.

Other languages are supported via extensions:

[Ada](src/lang-ada.js);
[Apollo](src/lang-apollo.js);
[Basic](src/lang-basic.js);
[Clojure](src/lang-clj.js);
Expand Down
59 changes: 59 additions & 0 deletions src/lang-ada.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @license
* Copyright (C) 2015 Felix Krause
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Contributed by Felix Krause <contact curl flyx dot org>

/**
* @fileoverview
* Registers a language handler for Ada 2012.
* To use, include prettify.js and this file in your HTML page.
* Then put your code in an HTML tag like
* <pre class="prettyprint lang-ada">(my Ada code)</pre>
*
* @author Felix Krause <contact curl flyx dot org>
*/

PR.registerLangHandler(
PR.createSimpleLexer(
[ //shortcutStylePatterns
// no escapes in Ada strings - double quotes are added by repeating them
// inside the string (" foo "" bar ")
[PR.PR_STRING, /^"([^"\r\n]|"")*"/, null, '"']
],
[ // fallthroughStylePatterns
// there are no multiline comments in Ada, but we can combine
// full-line comments into one
[PR.PR_COMMENT, /^--(?:[^\r\n]|(?:\r|\n){1,2}--)*/, null],
// a single character literal
// (should this rather be a PR_LITERAL? I don't think so)
[PR.PR_STRING, /^\'.\'/, null],
// PR_ATTRIB_NAME originally is for XML attributes, but it fits quite
// well here. It's important that this rule comes before the keywords,
// so that 'Access is parsed as an attribute, not as a keyword. We're
// doing some heuristic here by saying that attributes have to be at
// least 2 characters long, to avoid clash with character literals
[PR.PR_ATTRIB_NAME, /\'[A-Za-z_]{2,}/, null],
[PR.PR_KEYWORD, /^\b(?:abort|abs|abstract|accept|access|aliased|all|and|array|at|begin|body|case|constant|declare|delay|delta|digits|do|else|elsif|end|entry|exception|exit|for|function|generic|goto|if|in|interface|is|limited|loop|mod|new|not|null|of|or|others|out|overriding|package|pragma|private|procedure|protected|raise|range|record|rem|renames|requeue|return|reverse|select|separate|some|subtype|synchronized|tagged|task|terminate|then|type|until|use|when|while|with|xor)\b/i, null],
[PR.PR_PLAIN, /^\b[a-zA-Z](_|[a-zA-Z0-9])*\b/, null],
// numeric literals are quite complex,
// like 2_000_000, 1.34E-12, or 16#9A4E#.
[PR.PR_LITERAL, /^\b([0-9](_?[0-9])*((#[0-9a-f](_?[0-9a-f])*#((e(\+|-)?[0-9](_?[0-9])*\b)|\B))|((\.[0-9](_?[0-9])*)?(e(\+|-)?[0-9](_?[0-9])*)?\b)))\b/i, null],
// the box is a special literal of sorts.
[PR.PR_LITERAL, /<>/, null],
[PR.PR_PUNCTUATION, /^[\+\-\*\/&<>=:;\.\(\)\',]/, null]
]),
['ada']);
90 changes: 90 additions & 0 deletions tests/prettify_test_2.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// JS & CSS prettify files to load
var base = /[&?]loader\b/.test(location.search) ? '../loader/' : '../src/';
var sources = [
'lang-ada.js',
'lang-basic.js',
'lang-clj.js',
'lang-css.js',
Expand Down Expand Up @@ -579,6 +580,95 @@ <h1>Pascal</h1>
END;
</pre>


<h1>Ada</h1>
<pre class="prettyprint lang-ada" id="ada">
generic -- Example from Rosetta Code
type Element is private;
type Index is (&lt;&gt;);
type Element_Array is array(Index range &lt;&gt;) of Element;
with function "&lt;" (Left, Right : Element) return Boolean is &lt;&gt;;
procedure Quick_Sort(A : in out Element_Array);

-- The procedure body deals with any discrete index type, either an integer type or an enumerated type.

procedure Quick_Sort (A : in out Element_Array) is

procedure Swap(Left, Right : Index) is
Temp : Element := A (Left);
begin
A (Left) := A (Right);
A (Right) := Temp;
end Swap;

begin
if A'Length &gt; 1 then
declare
Pivot_Value : Element := A (A'First);
Right : Index := A'Last;
Left : Index := A'First;
begin
loop
while Left &lt; Right and not (Pivot_Value &lt; A (Left)) loop
Left := Index'Succ (Left);
end loop;
while Pivot_Value &lt; A (Right) loop
Right := Index'Pred (Right);
end loop;
exit when Right &lt;= Left;
Swap (Left, Right);
Left := Index'Succ (Left);
Right := Index'Pred (Right);
end loop;
if Right = A'Last then
Right := Index'Pred (Right);
Swap (A'First, A'Last);
end if;
if Left = A'First then
Left := Index'Succ (Left);
end if;
Quick_Sort (A (A'First .. Right));
Quick_Sort (A (Left .. A'Last));
end;
end if;
end Quick_Sort;

-- An example of how this procedure may be used is:

with Ada.Text_Io;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;

procedure Sort_Test is
type Days is (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
type Sales is array (Days range &lt;&gt;) of Float;
procedure Sort_Days is new Quick_Sort(Float, Days, Sales);

procedure Print (Item : Sales) is
begin
for I in Item'range loop
Put(Item =&gt; Item(I), Fore =&gt; 5, Aft =&gt; 2, Exp =&gt; 0);
end loop;
end Print;

Weekly_Sales : Sales :=
(Mon =&gt; 300.0,
Tue =&gt; 700.0,
Wed =&gt; 800.0,
Thu =&gt; 500.0,
Fri =&gt; 200.0,
Sat =&gt; 100.0,
Sun =&gt; 900.0);

begin

Print(Weekly_Sales);
Ada.Text_Io.New_Line(2);
Sort_Days(Weekly_Sales);
Print(Weekly_Sales);

end Sort_Test;
</pre>

<h1>BASIC</h1>
<pre class="prettyprint lang-basic" id="basic_lang">
200 REM ----- method teardown
Expand Down