2 * Copyright (c) 2010 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /** @file Coordinate span
31 * Captures the origin (input object, starting and ending line number and
32 * columnt number) of a code fragment.
42 /** Allocate new coordinate span.
44 * @param input Input object.
45 * @param line Starting line number.
46 * @param col0 Starting column number.
47 * @param line Ending number (inclusive).
48 * @param col1 Ending column number (inclusive).
50 * @return New coordinate span.
52 cspan_t
*cspan_new(input_t
*input
, int line0
, int col0
, int line1
, int col1
)
56 cspan
= calloc(1, sizeof(cspan_t
));
58 printf("Memory allocation failed.\n");
71 /** Create a merged coordinate span.
73 * Creates the smalles cspan covering cpans @a a and @a b. Both spans
74 * must be from the same input object and @a a must start earlier
75 * than @a b terminates.
77 * @param a First coordinate span
78 * @param b Second coordinate span
79 * @return New coordinate span.
81 cspan_t
*cspan_merge(cspan_t
*a
, cspan_t
*b
)
85 assert(a
->input
== b
->input
);
87 return cspan_new(a
->input
, a
->line0
, a
->col0
, b
->line1
, b
->col1
);
90 /** Print coordinate span.
92 * @param cspan Coordinate span
94 void cspan_print(cspan_t
*cspan
)
96 printf("%s:", cspan
->input
->name
);
98 if (cspan
->line0
!= cspan
->line1
) {
99 printf("%d:%d-%d:%d", cspan
->line0
, cspan
->col0
, cspan
->line1
,
102 printf("%d:%d-%d", cspan
->line0
, cspan
->col0
, cspan
->col1
);