From 3eb1d916fa7e6c115e256a44786f40751cb05dac Mon Sep 17 00:00:00 2001 From: mgr Date: Sun, 17 Nov 2019 00:35:33 +0100 Subject: [PATCH 1/2] Support for Ada Patch provided by Felix Krause in issue #312 --- src/lang-ada.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/lang-ada.js diff --git a/src/lang-ada.js b/src/lang-ada.js new file mode 100644 index 00000000..ee41b32f --- /dev/null +++ b/src/lang-ada.js @@ -0,0 +1,39 @@ +// Contributed by Felix Krause + +/** + * @fileoverview + * Registers a language handler for Ada 2012. + * + * @author Felix Krause + */ + +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']); \ No newline at end of file From 050b9681a20fc7d0576d13ce7e83b0a3c7936f44 Mon Sep 17 00:00:00 2001 From: mgr Date: Sun, 17 Nov 2019 01:01:55 +0100 Subject: [PATCH 2/2] Update Ada support - Added to README - Updated header - Added Ada source code to test Fixes issue #312 --- README.md | 1 + src/lang-ada.js | 20 +++++++++ tests/prettify_test_2.html | 90 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) diff --git a/README.md b/README.md index 4f89b092..4833db88 100644 --- a/README.md +++ b/README.md @@ -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); diff --git a/src/lang-ada.js b/src/lang-ada.js index ee41b32f..318343a9 100644 --- a/src/lang-ada.js +++ b/src/lang-ada.js @@ -1,8 +1,28 @@ +/** + * @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 /** * @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 + *
(my Ada code)
* * @author Felix Krause */ diff --git a/tests/prettify_test_2.html b/tests/prettify_test_2.html index c62e032b..86da768f 100644 --- a/tests/prettify_test_2.html +++ b/tests/prettify_test_2.html @@ -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', @@ -579,6 +580,95 @@

Pascal

END; + +

Ada

+
+generic -- Example from Rosetta Code
+   type Element is private;
+   type Index is (<>);
+   type Element_Array is array(Index range <>) of Element;
+   with function "<" (Left, Right : Element) return Boolean is <>;
+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 > 1 then
+   declare
+      Pivot_Value : Element := A (A'First);
+      Right       : Index := A'Last;
+      Left        : Index := A'First;
+   begin
+       loop
+          while Left < Right and not (Pivot_Value < A (Left)) loop
+             Left := Index'Succ (Left);
+          end loop;
+          while Pivot_Value < A (Right) loop
+             Right := Index'Pred (Right);
+          end loop;
+          exit when Right <= 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 <>) 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 => Item(I), Fore => 5, Aft => 2, Exp => 0);
+      end loop;
+   end Print;
+
+   Weekly_Sales : Sales :=
+     (Mon => 300.0,
+      Tue => 700.0,
+      Wed => 800.0,
+      Thu => 500.0,
+      Fri => 200.0,
+      Sat => 100.0,
+      Sun => 900.0);
+
+begin
+
+   Print(Weekly_Sales);
+   Ada.Text_Io.New_Line(2);
+   Sort_Days(Weekly_Sales);
+   Print(Weekly_Sales);
+
+end Sort_Test;
+
+

BASIC

 200 REM ----- method teardown